2010-05-03 22 views
5

Sorun şu: Bir sayfada aynı türde 2 denetim eklediğimde, farklı önek belirtmek zorundayım. Bu durumda, formdan sonra doğrulanan doğrulama kuralları yanlıştır. Yani durumda ?: için istemci doğrulama çalışmaları nasılAsp.Net MVC2 Önekler ile denetimlerle istemci kimlik doğrulaması sorunu

sayfa içerir:

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

kontrol ViewUserControl <PhoneViewModel>: Model.GetPrefixed("CountryCode") sadece "FaxPhone.CountryCode" döndürür

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

veya

'a bağlı olarak "PhonePhone.CountryCode"

Ve bu formdan sonra oluşturulan doğrulama kuralları. Onlar "Phone.CountryCode" alan adı için çoğaltılır. İstenen sonuç 2 kurallar FieldNames "FaxPhone.CountryCode" her biri için (gerekli, sayı), "PhonePhone.CountryCode" alt text http://www.freeimagehosting.net/uploads/37fbe720bf.png

iken soru biraz Asp.Net MVC2 Clientside Validation and duplicate ID's problem arasında çoğaltmak ancak tavsiye elle kimlikleri doesn oluşturmak için yardımcı olur.

cevap

10

metin kutusu ve doğrulama için her ikisi de aynı önekler ayarlamak için doğru yol:

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

(tesadüfen Steve Sanderson'ın blogunda http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/ kodda çözüm buldu)

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

Ayrıca Html.EditorFor yaklaşımı gibi önerdiği gibi çalışmalıdır: ASP.NET MVC 2 - ViewModel Prefix

+0

Güzel. Bu cevap çok yardımcı oldu. Keşke bir kaç kez daha yapabilirim. –

+0

Çok, çok yararlı. Teşekkür ederim. – Luke

+0

Bunun eski bir yanıt olduğunu biliyorum, ancak yönteminizin bunu sizin görünümünüze koymaya tercih edip etmediğini merak ediyorum: ViewData.TemplateInfo.HtmlFieldPrefix = "myViewModel.MyCustomObjdect"; –

İlgili konular