处理动态生成的表中的按钮点击
<%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
属性传递到处理按钮单击的任何方法中。
谢谢!
<%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>
<%} %>
So that above table, as you can see, is dynamically generated. How do I handle the button click? I also want to pass the name
attribute of the button into whatever method handles the button click.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用jQuery的live功能。
试试这个:
You can use the live function of jQuery.
Try this:
与处理常规按钮单击的方式相同。动态创建代码来处理您生成的 http 代码中的常规按钮点击。
The same way you would handle a regular button click. Dynamically create the code to handle regular button clicks in the http code you're generating.
这段代码是悲剧性的,需要重构。光是看着就觉得眼睛疼。您没有对字符串进行编码,因此使该代码容易受到 XSS 攻击。
因此,与往常一样,在 ASP.NET MVC 中,您从视图模型开始:
然后您将拥有一个控制器操作,它将从存储库中获取模型并将其映射到视图模型。您可以使用 AutoMapper 来简化此映射。在映射层中,您将转换所有内容以供视图直接使用,这样该视图就不会类似于可怕的标签汤:
接下来我们进入强类型视图,我们将在其中使用显示模板:
并在相应的显示模板(
~/Views/Shared/DisplayTemplates/MyViewModel.ascx
)中:最后您可以使用 jquery 附加到此按钮的单击并获取名称:
That code is tragic and screaming for refactoring. Just looking at it my eyes hurt. You are not encoding strings thus making this code vulnerable to XSS attacks.
So as always in ASP.NET MVC you start with a view model:
then you will have a controller action which will fetch the model from the repository and map it to the view model. You could use AutoMapper to simplify this mapping. It's in the mapping layer that you will transform everything to be ready to be directly used by the view so that this views doesn't resemble to a horrible tag soup:
next we get to the strongly typed view where we will be using Display Templates:
and in the corresponding display template (
~/Views/Shared/DisplayTemplates/MyViewModel.ascx
):and finally you could use jquery to attach to the click of this button and fetch the name: