Hi how are you? I'm trying to validate a form in ASP NET MVC.
I have a partial view "Address" that I reuse for some entities, like Company, Person, etc.
My problem is that when I submit the form, only the controls of the father view gets validated, the ones in the partial view don't.
Here's some code I hope u can geive me hand
PERSON VIEW
@model Entities.Person
@using (Html.BeginForm("Create", "Person", FormMethod.Post))
{
<table>
<tr>
<td>
@Html.LabelFor(model => model.FirstName)
<div class="control">
@Html.TextBoxFor(model => model.FirstName, new { @maxlength = 7, @class = "numeric"})
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="spacer-short"></div>
</td>
<td>
@Html.LabelFor(model => model.LastName)
<div class="control">
@Html.TextBoxFor(model => model.LastName, new { @maxlength = 7, @class = "numeric"})
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="spacer-short"></div>
</td>
</tr>
</table>
@{ Html.RenderAction("Index", "Address", new {id = Model.AddressId});} //Renders the Address form part
<div class="spacer"></div>
<button type="submit" data-button="true" data-button-icon="ui-icon-disk" class="">Create</button>
<button type="button" data-button="true" data-button-icon="ui-icon-circle-close" class="button-cancel">Cancel</button>
}
ADDRESS VIEW
@model Entities.Address
<table>
<tr>
<td>
@Html.LabelFor(model => model.Street)
<div class="control">
@Html.TextBox("Address.Street", Model.Street)
@Html.ValidationMessage("Address.Street")
</div>
<div class="spacer-short"></div>
</td>
<td>
@Html.LabelFor(model => model.Number)
<div class="control">
@Html.TextBox("Address.Number", Model.Number)
@Html.ValidationMessage("Address.Number")
</div>
<div class="spacer-short"></div>
</td>
</tr>
</table>
Then I have some validation metadata ([Required]) for person and address fields
Try this:
@Html.EditorFor(Model.Street)
@Html.ValidationMessageFor(Model.Street)
instead of this:
@Html.TextBox("Address.Street", Model.Street)
@Html.ValidationMessage("Address.Street")