<%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>
<%} %>
因此,您可以看到,上表是动态生成的。如何处理按钮点击?我还想将按钮的name
属性传递给处理按钮单击的任何方法。
谢谢!
答案 0 :(得分:3)
你可以使用jQuery的live函数。
试试这个:
$(function(){
$("td input[type=button][value=Open]").live("click", function(e){
var btn = $(this);
alert(btn.attr("name"));
});
})
答案 1 :(得分:0)
与处理常规按钮点击的方式相同。动态创建代码以处理您正在生成的http代码中的常规按钮单击。
答案 2 :(得分:0)
该代码是悲剧性的,并且正在为重构而尖叫。只是看着它,我的眼睛受伤了。您没有编码字符串,因此使此代码容易受到XSS攻击。
因此,在ASP.NET MVC中,您始终使用视图模型:
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; }
}
然后您将拥有一个控制器操作,该操作将从存储库中获取模型并将其映射到视图模型。您可以使用AutoMapper来简化此映射。在映射层中,您将转换所有内容以供视图直接使用,以便此视图与可怕的标记汤不相似:
public ActionResult Foo()
{
// it's here that you should do the LINQ queries, etc ...
// not in the view. Views are not supposed to fetch any data
// and to be intelligent. Views should be dumb and only render
// the preformatted data that they have been fed by the controller action
IEnumerable<SomeModel> model = ...
IEnumerable<MyViewModel> viewModel = Mapper.Map<IEnumerable<SomeModel>, IEnumerable<MyViewModel>>(model);
return View(viewModel);
}
接下来我们将进入强类型视图,我们将使用显示模板:
<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>
并在相应的显示模板(~/Views/Shared/DisplayTemplates/MyViewModel.ascx
)中:
<%@ 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>
最后你可以使用jquery附加到单击此按钮并获取名称:
$(function() {
$('#myTable button').click(function() {
var btn = $(this);
alert(btn.attr('name'));
});
});