你好你好吗?我正在尝试验证 ASP NET MVC 中的表单。
我有一个部分视图“地址”,可用于某些实体,如公司、个人等。
我的问题是,当我提交表单时,只有父视图的控件得到验证,部分视图中的控件没有。
这是一些代码,我希望你能帮我
人物视图
@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>
}
地址视图
@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>
然后我有一些用于人员和地址字段的验证元数据([必需])