2012-03-02 45 views

回答

1

您可以创建类似的HtmlHelper:使用JQuery

@Html.Button("CustomButton", "http://localhost") 

的JS:

$(function() { 
    $(".custom-button").click(function() { 
     // This will handle all the click events of the buttons 
    }); 

    $("#CustomButton").click(function() { 
     // This will handle the click event on the specific button 
    }); 
}); 

正如达林说,有没有更多的用户

public static class Helpers { 
    public static MvcHtmlString Button(this HtmlHelper html, string id, string url) { 
     var builder = new TagBuilder("a"); 

     builder.MergeAttribute("href", url); 
     builder.MergeAttribute("id", id); 
     builder.AddCssClass("custom-button"); 

     return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal)); 
    } 
} 

在视图控件就像我们在asp.net webforms中一样,根据我的说法,这是一件非常好的事情。

所以上面的代码是一种构建“用户控件”的方法。

+0

太棒了!非常感谢 – kbaccouche 2012-03-02 13:47:36

2

ASP.NET MVC中不存在服务器端控件和事件。如果你想跟踪事件,如oncilckonhover你可以使用JavaScript。你也可以使用HTML助手。只需在ASP.NET MVC应用程序中忘记System.Web.UI命名空间。