2011-04-11 4 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

+0

또한, .delegate는 1.4.2 이후의 jquery와 바인딩하는 방법이 멋지고 타겟이 명확합니다. http://brandonaaron.net/blog/2010/03/4/event-delegation-with-jquery –

+0

@Zach : 링크 Thx. – Chandu

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')); 
    }); 
}); 
관련 문제