我正在使用MVC 3.0
我的问题是在一个页面上我使用相同的模型两次进行一些验证 . 但客户端验证仅适用于第一个模型 .
我的代码是
@using (Html.BeginDTPanel("Applicant"))
{
<text>
@Html.Partial("~/areas/common/views/shared/_customer.cshtml", Model.Applicant)
</text>
}
@{ var state = Model.Mode == ActionMode.Edit && Model.CoApplicant.TaxIdentifierLastFour != null ? "expanded" : "collapsed"; }
@using (Html.BeginDTPanel("Co-applicant", state))
{
<text>
@Html.Partial("~/areas/common/views/shared/_customer.cshtml", Model.CoApplicant)
</text>
}
_Customer.cshtml code is something like
@Html.LabelFor(Model.Prefix, m => m.FirstName, "First Name")
@ Html.TextBoxFor(Model.Prefix,m => m.FirstName)@ Html.ValidationMessageFor(Model.Prefix,m => m.FirstName)
@ Html.LabelFor(Model.Prefix,m => m.MiddleName,"Middle Initial")@ Html.TextBoxFor(Model.Prefix,m => m.MiddleName)@ Html.ValidationMessageFor(Model.Prefix,m => m.MiddleName)
@ Html.LabelFor(Model.Prefix,m => m.LastName,"Last Name")@ Html.TextBoxFor(Model.Prefix,m => m.LastName)@ Html.ValidationMessageFor(Model.Prefix,m => m.LastName)
Validation model which I used is as below
[RequiredIf(ErrorMessage = "Please Enter First Name")] [StringLength(15,ErrorMessage = "Maximum character limit exceeded")] [RegularExpression(@“^ [a-zA-Z0-9]((['\,.-] [a-zA-Z0- 9])?[a-zA-Z0-9])$ ", ErrorMessage = "不正确的名字“)] public string FirstName {get;组; }
[StringLength(1, ErrorMessage = "Maximum character limit exceeded")]
[RegularExpression(@"^[a-zA-Z ]$", ErrorMessage = "Incorrect Middle Initial")]
public string MiddleName { get; set; }
[RequiredIf(ErrorMessage = "Please Enter Last Name")]
[StringLength(25, ErrorMessage = "Maximum character limit exceeded")]
[RegularExpression(@"^[a-zA-Z0-9 ]+(([\'\,\.\-][a-zA-Z0-9 ])?[a-zA-Z0-9 ]*)*$", ErrorMessage = "Incorrect Last Name")]
public string LastName { get; set; }
[RequiredIf(ErrorMessage = "Please Enter SSN")]
[StringLength(11, ErrorMessage = "Maximum character limit exceeded")]
[SouciaSecurityNumber(ErrorMessage ="Invalid SSN")]
[RegularExpression(@"^([0-9]\d{2}|7[0-6]\d|77[0-2])([ \-]?)(\d{2})\2(\d{4})$", ErrorMessage = "InValid SSN")]
public string TaxIdentifier { get; set; }
确切的问题在这里我使用相同的模型与申请人和共同申请人的验证 . 但是,当页面呈现验证仅适用于第一个申请人时 .
当我检查仅为申请人呈现的查看源验证相关代码时 .
但是我需要为这两种模式应用验证 .
请建议你有任何解决方案 .
谢谢
2 回答
使用编辑器模板而不是标准部分 . 基本上只需将
_customer.cshtml
移动到/Shared/EditorTemplates/Customer.cshtml
然后再使用A确实在我的.cshtml文件中添加了以下内容 .
@ {ViewData.TemplateInfo = new System.Web.Mvc.TemplateInfo ; }