I have a partial twice on a view. Partial doesnt use a loop and has basic validation.
Here is the partial view code:
<div class="col-md-4">
@Html.LabelFor(model => model.ZipCode, new { @class = "control-label " })
@Html.TextBoxFor(model => model.ZipCode, new { @class = "form-control ", tabindex = "4" })
@Html.ValidationMessageFor(model => model.ZipCode)
</div>
Here is my main view calling it twice:
<div id="homeaddress">
@if (Model == null)
{
@Html.Partial("~/Views/AddrPartial.cshtml", new Address())
}
else
{
@Html.Partial("~/Views/AddrPartial.cshtml", Model.HomeAddress)
}
</div>
<div id="mailingaddress">
@if (Model == null)
{
@Html.Partial("~/Views/AddrPartial.cshtml", new Address())
}
else
{
@Html.Partial("~/Views/AddrPartial.cshtml", Model.MailingAddress)
}
</div>
Then only the "homeadrress" div validation works... here's how my model is setup:
public class Information
{
public Address HomeAddress { get; set; }
public Address MailingAddress { get; set; }
}
Then have a separate Address class...
public class Address
{
[Display(Name = "Address")]
public string Addr1 { get; set; }
[Display(Name = "Address 2")]
public string Addr2 { get; set; }
[Display(Name = "Zip Code")]
[RegularExpression(@"^\d{5}(-\d{4})?|^$", ErrorMessage = "Invalid Zip Code")]
public string ZipCode { get; set; }
}
The html generated shows the problem... the mailingaddress html doesnt have the regex necessary to check validation..
<input class="form-control " data-val="true" data-val-regex="Invalid Zip Code" data-val-regex-pattern="^\d{5}(-\d{4})?|^$" id="ZipCode_home" name="ZipCode_home" tabindex="4" type="text" value="12345">
<span class="field-validation-valid" data-valmsg-for="ZipCode_home" data-valmsg-replace="true"></span>
<input class="form-control " id="ZipCode_mailing" name="ZipCode_mailing" tabindex="4" type="text" value="54321">
<span class="field-validation-valid" data-valmsg-for="ZipCode_mailing" data-valmsg-replace="true"></span>
after reviewing this code my question is why is this happening and how can i fix this. Thanks in advance I can answer questions and provide more code if need be.