Model Validation problem when render same model twice pn same page
I am Using MVC 3.0
My issue is on one page I am using same model twice with some validation. But client side validation are gets applied for first model only.
My code in view is
@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 = "Incorrect First Name")] public string FirstName { get; set; }
[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; }
The exact issues is here I am using same model with validation for both applicant and coapplicant. but when page render validation actully applies to first Applicant only.
When I checked view source validation related code rendered for Applicant only.
But I need to apply validation for both the mode.
Please suggest any solution you have.
Thanks
Use editor templates instead of standard partials. Basically just move _customer.cshtml
to /Shared/EditorTemplates/Customer.cshtml
and then use
@Html.EditorFor(m => m.Applicant)
@Html.EditorFor(m => m.CoApplicant)
A did add follwing line to my .cshtml file its working now.
@{ ViewData.TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = Model.Prefix }; }
精彩评论