2011-04-11 147 views
2
<%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屬性傳遞給處理按鈕點擊的任何方法。處理按鈕點擊

謝謝!

回答

3

您可以使用jQuery的實時功能。

試試這個:

$(function(){ 
    $("td input[type=button][value=Open]").live("click", function(e){ 
     var btn = $(this); 
     alert(btn.attr("name")); 
    }); 
}) 
+0

不錯!謝謝! – slandau 2011-04-11 15:50:55

+0

另外,請記住.delegate更好,更有針對性,在1.4.2之後綁定jquery。 http://brandonaaron.net/blog/2010/03/4/event-delegation-with-jquery – 2011-04-11 16:25:27

+0

@Zach:Thx的鏈接。 – Chandu 2011-04-11 16:45:02

0

你會處理一個普通按鈕點擊相同的方式。動態創建代碼來處理您生成的http代碼中的常規按鈕點擊。

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')); 
    }); 
});