2012-08-23 24 views
1

我是新來mvc.I創建的HTML按鈕動態,我無法爲這些按鈕分配動作,以動態創建HTML按鈕MVC3

代碼指定動作:

sb.Append("<li style=\"margin:17px;\">"); 
     sb.Append("<img src=\"" + path + "Content/HeaderSlides/FullImages/" 
        + imagename + "\" width=\"180\" height=\"100\"/>"); 

     sb.Append("<input type=\"button\" name=\"deleteimage" + id 
        + " \" id=\"btndelete" + id 
        + "\" value=\"Delete\" class=\"t-button t-grid-delete\" " 
        + " style=\"margin-left:10px;\" />"); 

我想使HTML按鈕是這樣的:

<input type="submit" 
     title="Delete" 
     value="Delete" 
     name="btndelete" 
     onclick="location.href='@Url.Action("DeleteHeaderSlide", 
              "HeaderSlides", 
              new { filename = "Sunset.jpg" })'" /> 
+0

只給出一個按鈕創建的完整代碼。所有其他人也一樣 – Sami

+0

你爲什麼用字符串創建html?使用[助手或模板]要好得多(http://stackoverflow.com/questions/5037580/asp-net-mvc-3-partial-vs-display-template-vs-editor-template)。另外,如果你的按鈕只是改變當前頁面,爲什麼不使用鏈接? – jrummell

回答

1

你最好使用jquery。

$(document).ready(function(){ 
    $('input[name=btndelete]').bind('click', function() { 
     window.location.href='@Url.Action("DeleteHeaderSlide", 
             "HeaderSlides", 
             new { filename = "Sunset.jpg" })'; 
    }); 

}); 
0

如果你能夠與你的代碼創建HTML按鈕定義最簡單的方法/分配動作

string action = "<script>function yourFunction() {alert('hi');}</script>"; 
string btnCraeteCode = "<input type='button' onClick='yourFunction()' />";  
sub.Append(action+btnCraeteCode); 

您也可以已經定義yourFunction中()在你的頁面

的頭標記在這種情況下,在腳本中定義的,你不需要在這裏添加操作字符串createButton