2010-05-03 30 views
5

Vấn đề là: khi tôi đặt 2 điều khiển cùng loại trên một trang tôi cần phải xác định tiền tố khác nhau để ràng buộc. Trong trường hợp này, các quy tắc xác nhận được tạo ngay sau khi biểu mẫu không chính xác. Vậy làm thế nào để có được công việc xác nhận của khách hàng đối với trường hợp ?:Asp.Net MVC2 Clientside xác nhận vấn đề với điều khiển với tiền tố

trang chứa:

<% 
    Html.RenderPartial(ViewLocations.Shared.PhoneEditPartial, new PhoneViewModel { Phone = person.PhonePhone, Prefix = "PhonePhone" }); 
    Html.RenderPartial(ViewLocations.Shared.PhoneEditPartial, new PhoneViewModel { Phone = person.FaxPhone, Prefix = "FaxPhone" }); 
%> 

các ViewUserControl kiểm soát <PhoneViewModel>:

<%= Html.TextBox(Model.GetPrefixed("CountryCode"), Model.Phone.CountryCode) %> 
<%= Html.ValidationMessage("Phone.CountryCode", new { id = Model.GetPrefixed("CountryCode"), name = Model.GetPrefixed("CountryCode") })%> 

nơi Model.GetPrefixed("CountryCode") chỉ trả về "FaxPhone.CountryCode" hoặc "PhonePhone.CountryCode" tùy thuộc vào tiền tố


Và đây là quy tắc xác thực được tạo sau biểu mẫu. Chúng được nhân đôi cho tên trường "Phone.CountryCode". Trong khi kết quả mong muốn là 2 quy tắc (bắt buộc, số) cho mỗi fieldnames "FaxPhone.CountryCode", "PhonePhone.CountryCode" alt text http://www.freeimagehosting.net/uploads/37fbe720bf.png

Câu hỏi đặt ra là hơi trùng lặp của Asp.Net MVC2 Clientside Validation and duplicate ID's problem nhưng lời khuyên để tự tạo id doesn không giúp gì cả.

Trả lời

10

cách chính xác để thiết lập các tiền tố cùng cho cả hộp và xác nhận:

<% using (Html.BeginHtmlFieldPrefixScope(Model.Prefix)) { %> 
    <%= Html.TextBoxFor(m => m.Address.PostCode) %> 
    <%= Html.ValidationMessageFor(m => m.Address.PostCode) %> 
<% } %> 

nơi

public static class HtmlPrefixScopeExtensions 
{ 
    public static IDisposable BeginHtmlFieldPrefixScope(this HtmlHelper html, string htmlFieldPrefix) 
    { 
     return new HtmlFieldPrefixScope(html.ViewData.TemplateInfo, htmlFieldPrefix); 
    } 

    private class HtmlFieldPrefixScope : IDisposable 
    { 
     private readonly TemplateInfo templateInfo; 
     private readonly string previousHtmlFieldPrefix; 

     public HtmlFieldPrefixScope(TemplateInfo templateInfo, string htmlFieldPrefix) 
     { 
      this.templateInfo = templateInfo; 

      previousHtmlFieldPrefix = templateInfo.HtmlFieldPrefix; 
      templateInfo.HtmlFieldPrefix = htmlFieldPrefix; 
     } 

     public void Dispose() 
     { 
      templateInfo.HtmlFieldPrefix = previousHtmlFieldPrefix; 
     } 
    } 
} 

(một cách tình cờ tìm thấy giải pháp trong các mã trên Steve Sanderson của blog http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/)

Cũng giống như cách tiếp cận Html.EditorFor cũng hoạt động như được đề xuất ở đây: ASP.NET MVC 2 - ViewModel Prefix

+0

Tốt. Câu trả lời này rất hữu ích. Ước gì tôi có thể upvote nó một vài lần nữa. –

+0

Rất, rất hữu ích. Cảm ơn bạn. – Luke

+0

Tôi biết đây là một câu trả lời cũ, nhưng tôi đã tự hỏi nếu phương pháp của bạn là thích hợp hơn để đặt này trong quan điểm của bạn: ViewData.TemplateInfo.HtmlFieldPrefix = "myViewModel.MyCustomObjdect"; –

Các vấn đề liên quan