我有部分视图“地址”,我重复使用某些实体,如公司,人等等。
我的问题是,当我提交表单时,只有父视图的控件才会被验证,部分视图中的控件不会被验证。
这里有一些代码,我希望你能帮我一臂之力
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>
然后我为人员和地址字段提供了一些验证元数据([必需])
答案 0 :(得分:8)
使用@Html.Partial(...)
并期望显示验证错误的常见错误是没有将ViewData传递给该部分。:
@Html.Partial([ViewName], [EmailAddressObject], ViewData)
答案 1 :(得分:0)
尝试使用Html.Partial(...)
代替Html.RenderAction(...)
来渲染局部视图。这可能会抑制验证。
答案 2 :(得分:0)
试试这个:
@Html.EditorFor(Model.Street)
@Html.ValidationMessageFor(Model.Street)
而不是:
@Html.TextBox("Address.Street", Model.Street)
@Html.ValidationMessage("Address.Street")