且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Ajax调用中断了对子对象的绑定

更新时间:2023-11-21 12:05:10

通过在局部视图中生成输入字段,HTML帮助程序没有意识到Address模型是初始Client模型的属性,因此它在生成HTML输入像:

By generating your input fields in the partial view, the HTML helpers are unaware that your Address model is a property of your initial Client model, so it's generating HTML inputs like:

<input type="text" id="City" name="City" />
<input type="text" id="State" name="State" />

如果您的POST操作方法正在接受客户端模型,则模型绑定器将查找不存在的客户端模型属性城市"和州".

If your POST action method is accepting a Client model then the model binder will look for the properties City and State of the Client model, which don't exist.

您需要HTML输入如下所示:

You need your HTML input to look like:

<input type="text" id="Address_City" name="Address.City" />
<input type="text" id="Address_State" name="Address.State" />

您应该使用编辑器模板,它将在这种情况下根据需要保留父属性.

Instead of using a partial for your Address fields, you should use an Editor Template which will then preserve the parent property as you need in this case.

@Html.EditorFor(x => x.Address)