当视图上存在两个用户控件实例时,验证不起作用

时间:2010-08-17 14:06:29

标签: asp.net-mvc validation

我创建了一个地址用户控件,如下所示:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Address>" %>

   <div class="span-4" style="text-align: right" title="<%: GetLocalResourceObject("Type") %>">
        <%: GetLocalResourceObject("Type")%>
   </div>
   <div class="span-6 last">
       <%: Html.RadioButtonFor(model => model.AddressType, RealProperty.Core.Domain.AddressType.Postal, new { id = ViewData["AddressPrefix"] + "AddressType" })%>
    &nbsp;
       <%: GetLocalResourceObject("PostalType")%>
       <%: Html.RadioButtonFor(model => model.AddressType, RealProperty.Core.Domain.AddressType.Intersection, new { id = ViewData["AddressPrefix"] + "AddressType" })%>
    &nbsp;
       <%: GetLocalResourceObject("IntersectionType")%>
    </div>
    <div class="span-4" style="text-align: right" title="<%: GetLocalResourceObject("Country") %>">
      <%: GetLocalResourceObject("Country")%>
    </div>
    <div class="span-6 last">
    <%: Html.DropDownList(ViewData["AddressPrefix"] + "Country", new SelectList(RealProperty.Core.Service.CountryService.Countries, "Id", "Name"), "Please select Country", new { id = ViewData["AddressPrefix"] + "Country" })%>
        <%: Html.ValidationMessage(ViewData["AddressPrefix"] + "Country")%>
    </div>
    <div class="span-4" style="text-align: right" title="<%: GetLocalResourceObject("Address1") %>">
       <%: GetLocalResourceObject("Address1")%>
    </div>

 .
 .
 .
 .

我将在“主要地址”和“备用地址”的页面上使用此用户控件的两个实例:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PhysicalLocation>" %>

   <div class="span-12">
        <fieldset>
            <legend>Main Address</legend>
            <%
                Html.RenderPartial("~/Views/Address/New.ascx", Model.MainAddress);
            %>
        </fieldset>
    </div>
    <div class="span-12 last">
        <fieldset>
            <legend>Alternate Address</legend>
            <%
                Html.RenderPartial("~/Views/Address/New.ascx", Model.AlternateAddress);
            %>
        </fieldset>
    </div>

地址用户控件绑定到“地址”模型。

 [PostalCode("PostalCode", "Country", ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "PostalCode_Is_Invalid"), DisplayName("Address") ]
    public class Address : ICanBeValidated
    {

        public virtual long Id { get; set; }

        [RegularExpression("^[^,'\"]*$", ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "InvalidCharacter"),
    Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Field_Required"), StringLength(255, ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Field_Max_Length_Allowed")]
        public virtual string Address1 { get; set; }

        [RegularExpression("^[^,'\"]*$", ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "InvalidCharacter"),
    StringLength(255, ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Field_Max_Length_Allowed")]
        public virtual string Address2 { get; set; }

        [RegularExpression("^([a-zA-Z\\.\\-\\s]*)$", ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "ValidCharacter_Allowed")]
        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Field_Required"), StringLength(30, ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Field_Max_Length_Allowed")]
        public virtual string City { get; set; }
    .
    .
    .
 }

我希望在渲染时,它为MainAddress和AlternateAddress创建两组不同的输入控件(即来自Web Form体验)。

我猜我在这里错过了一个非常基本的观点,因为当两个用户控件都被渲染时,在页面上我有两个“city”,“address1”,“address2”等等。

结果Microsoft Validation仅适用于其中一个用户控件。发送到客户端的JSON数组仅包含模型定义的验证。例如,对于地址的“City”属性,我们在所提到的数组中只有一个条目,因此只有与验证相关的id为“City_ValidateMessage”的“span”元素存在。

我很感激。

谢谢, 穆罕默德

2 个答案:

答案 0 :(得分:0)

如果您在页面上使用了2个实例,那么您的模型是否有2组属性来存储地址?

你的模特应该是:

Class DoubleAddressViewModel()
    <PropertiesForAddressMain>
    <PropertiesForAddressAlternate>
End Class

从上面的评论中,我猜你有一个代表单个地址的模型

答案 1 :(得分:0)

我在Steve Sanderson's blog上找到的解决方案是对HttpContext的攻击,以在呈现用户控件时生成唯一ID。它很好地解决了验证问题。