Asp.net mvc 回发时未绑定模型内的对象
我有一个包含以下内容的局部视图:Asp.net mvc 回发时未绑定模型内的对象,asp.net-mvc,Asp.net Mvc,我有一个包含以下内容的局部视图: @model RegisterInputModel @using (Ajax.BeginForm("Register","Account", null, new AjaxOptions { UpdateTargetId = "signup-partial-update", HttpMethod = "POST" }, new { id = "js-form-signup" })) { @Html.EditorFor(x=>x.L
@model RegisterInputModel
@using (Ajax.BeginForm("Register","Account", null, new AjaxOptions
{
UpdateTargetId = "signup-partial-update",
HttpMethod = "POST"
}, new { id = "js-form-signup" }))
{
@Html.EditorFor(x=>x.Location,"_RenderLocationInputs")
// some more fields
}
然后是我的_RenderLocationInputs模板(在VIews/Shared/EditorTemplates/中)
我的注册计算机型号:
public class RegisterInputModel : InputModel
{
public Location Location { get; set; }
// other fields and ctor
}
当检查chrome POST时,我看到值已正确发送。一个例子:
地点。城市:布拉布拉
虽然他们不受我模式的约束。这不是EditorFor模板的使用方式吗
我不确定你需要什么代码。只需询问您是否需要更多信息。问题:当局部视图模型不是位置而是包含位置(和其他内容)的另一个模型时,我可以绑定到RegisterInputModel.Location吗 绑定基本上取决于
input
的name
属性,因为它会发布到服务器。所以您可以在客户端更改它,以不同的方式绑定
另一件事是从HttpContext
对象手动获取控制器上的值
此外,您可以编写自己的
ModelBinder
作为尺寸标注,但我认为这对您的情况没有多大帮助。只需将完整的RegisterInputModel传递给您的部分用户并更改此代码即可
@Html.HiddenFor(x => x.City, new { id = "hidden-location-city" })
@Html.HiddenFor(x => x.State, new { id = "hidden-location-state" })
@Html.HiddenFor(x => x.Country, new { id = "hidden-location-country" })
@Html.HiddenFor(x => x.Id, new { id = "hidden-location-id" })
与
它应该可以工作。你能展示你的controlelr动作吗?你能展示什么html呈现
@html.HiddenFor(x=>x.City,new{id=“hidden location City”})吗?
最后?你的控制器动作看起来像什么?你的表单看起来像什么?我已经为大家添加了代码:)因为我认为一切都是正确的,你说它们没有绑定是什么意思?模型是位置
,它包含属性城市
,州
等。如果不包含名为位置
的属性。及其和编辑模板
,而不是部分!
[HttpPost]
public ActionResult Register(RegisterInputModel input)
{
if (!ModelState.IsValid) return PartialView("Widgets/Register/_RegisterInput", input);
}
public class RegisterInputModel : InputModel
{
public Location Location { get; set; }
// other fields and ctor
}
@Html.HiddenFor(x => x.City, new { id = "hidden-location-city" })
@Html.HiddenFor(x => x.State, new { id = "hidden-location-state" })
@Html.HiddenFor(x => x.Country, new { id = "hidden-location-country" })
@Html.HiddenFor(x => x.Id, new { id = "hidden-location-id" })
@Html.HiddenFor(x => x.Location.City, new { id = "hidden-location-city" })
@Html.HiddenFor(x => x.Location.State, new { id = "hidden-location-state" })
@Html.HiddenFor(x => x.Location.Country, new { id = "hidden-location-country" })
@Html.HiddenFor(x => x.Location.Id, new { id = "hidden-location-id" })