C# 在动态生成的表中处理按钮单击 m.模型!=null&&m.Model.Trx!=null).OrderBy(m=>m.Model.Trx.PrimarySponsor.Company)) { %>

C# 在动态生成的表中处理按钮单击 m.模型!=null&&m.Model.Trx!=null).OrderBy(m=>m.Model.Trx.PrimarySponsor.Company)) { %>,c#,jquery,html,asp.net-mvc-2,C#,Jquery,Html,Asp.net Mvc 2,如您所见,上表是动态生成的。我如何处理按钮单击?我还想将按钮的name属性传递到处理按钮单击的任何方法中 谢谢!您可以使用jQuery的live功能 试试这个: <%foreach (var indication in Model.FindAll(m => m.Model != null && m.Model.Trx != null).OrderBy(m => m.Model.Trx.PrimarySponsor.Company))

如您所见,上表是动态生成的。我如何处理按钮单击?我还想将按钮的
name
属性传递到处理按钮单击的任何方法中


谢谢!

您可以使用jQuery的live功能

试试这个:

<%foreach (var indication in Model.FindAll(m => m.Model != null && m.Model.Trx != null).OrderBy(m => m.Model.Trx.PrimarySponsor.Company))
              { %>
                <tr>
              <td><%= indication.DisplayUser %></td>
              <td><%= indication.ActiveIndicationUsers[0].FullName %></td>
              <td><%= string.IsNullOrEmpty(indication.Model.Trx.PrimarySponsor.Company) ? "Not Yet Saved" : indication.Model.Trx.PrimarySponsor.Company %></td>
              <td><%= indication.TimeOpened.ToString(Chatham.Web.Data.Constants.Format.DateTimeSecondsFormatString) %></td>
              <td><%= indication.Model.Trx.ProductCollection[0].ProductTypeFriendlyName %></td>
              <td><%= (!indication.Model.Trx.ID.HasValue) ? "Not Yet Saved" : indication.Model.Trx.ID.Value.ToString() %></td>
              <td><input type="button" value="Open" name="<%= (!indication.Model.Trx.ID.HasValue) ? "Not Yet Saved" : indication.Model.Trx.ID.Value.ToString() %>" /></td>

              </tr>
            <%} %>

与处理常规按钮单击的方式相同。动态创建代码以处理您正在生成的http代码中的常规按钮单击。

该代码是悲剧性的,需要重构。看着它,我的眼睛很痛。您没有对字符串进行编码,因此此代码容易受到XSS攻击

因此,在ASP.NET MVC中,您总是从视图模型开始:

$(function(){
    $("td input[type=button][value=Open]").live("click", function(e){
        var btn = $(this);
        alert(btn.attr("name"));
    });
})
然后,您将有一个控制器操作,该操作将从存储库获取模型并将其映射到视图模型。您可以使用该操作来简化此映射。在映射层中,您将转换所有内容,以便视图可以直接使用,以便此视图不会类似于可怕的标记汤:

public class MyViewModel
{
    public string DisplayUser { get; set; }
    public string ActiveIndicationsUserFullname { get; set; }
    public string Company { get; set; }
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}")]
    public DateTime TimeOpened { get; set; }
    public string TrxId { get; set; }
}
最后,您可以使用jquery附加到此按钮的单击并获取名称:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.MyViewModel>" %>
<tr>
    <td><%= Html.DisplayFor(x => x.DisplayUser) %></td>
    <td><%= Html.DisplayFor(x => x.ActiveIndicationsUserFullname) %></td>
    <td><%= Html.DisplayFor(x => x.Company) %></td>
    <td><%= Html.DisplayFor(x => x.TimeOpened) %></td>
    <td><%= Html.DisplayFor(x => x.TrxId) %></td>
    <td>
        <input type="button" value="Open" name="<%= Model.TrxId %>" />
    </td>
</tr>

另外,请记住,委托是在1.4.2之后与jquery绑定的一种更好、更具针对性的方式。
<table id="myTable">
    <thead>
        <tr>
            <th>DisplayUser</th>
            <th>ActiveIndicationsUserFullname</th>
            <th>Company</th>
            <th>TimeOpened</th>
            <th>TrxId</th>
        </tr>
    </thead>
    <tbody>
        <%= Html.DisplayForModel()
    </tbody>
</table>
<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.MyViewModel>" %>
<tr>
    <td><%= Html.DisplayFor(x => x.DisplayUser) %></td>
    <td><%= Html.DisplayFor(x => x.ActiveIndicationsUserFullname) %></td>
    <td><%= Html.DisplayFor(x => x.Company) %></td>
    <td><%= Html.DisplayFor(x => x.TimeOpened) %></td>
    <td><%= Html.DisplayFor(x => x.TrxId) %></td>
    <td>
        <input type="button" value="Open" name="<%= Model.TrxId %>" />
    </td>
</tr>
$(function() {
    $('#myTable button').click(function() {
        var btn = $(this);
        alert(btn.attr('name'));
    });
});