[英]ASP.NET MVC - Model not binding on Ajax.BeginForm in partial view
I have a partial View AddOptionPartial which uses a model OptionViewModel . 我有一个局部视图AddOptionPartial使用模型OptionViewModel 。 This partial view is loaded when its parent, Build is loaded. 加载其父级Build时将加载此局部视图。 This Build view uses the model BuildViewModel . 此Build视图使用模型BuildViewModel 。
The partial is loaded in using using @{Html.RenderPartial("AddOptionPartial", new OptionViewModel());}
. 使用@{Html.RenderPartial("AddOptionPartial", new OptionViewModel());}
来加载部分。
The partial looks like this: 部分看起来像这样:
@model SurveyService.ViewModels.OptionViewModel
@{
Layout = null;
}
@using (Ajax.BeginForm("AddOption", "Manage", new AjaxOptions { OnSuccess = "handleSavedOptionChoice(data)", HttpMethod = "Post" }))
{
<div class="form-group">
@Html.LabelFor(m => m.ChoiceText)
@Html.TextBoxFor(m => m.ChoiceValue, new { @class = "form-control" })
</div>
@Html.LabelFor(m => m.ChoiceValue)
<div class="form-group">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
@Html.RadioButtonFor(m => m.ChoiceValue, 1) True
</label>
<label class="btn btn-primary">
@Html.RadioButtonFor(m => m.ChoiceValue, 0) False
</label>
<label class="btn btn-primary active">
@Html.RadioButtonFor(m => m.ChoiceValue, 2, true) Text
</label>
</div>
</div>
<button type="submit">Create</button>
}
As you can see, i am trying to post this form using an Ajax call. 如您所见,我正在尝试使用Ajax调用发布此表单。 The call refers to this function: 该调用引用此函数:
public ActionResult AddOption(OptionViewModel model)
{
***content of function***
}
However, when debugging, the model is an OptionViewModel object containing no data. 但是,在调试时,模型是不包含数据的OptionViewModel对象。 I can not find a reason for this on SO, can someone please advise? 我在SO上找不到原因,有人可以建议吗?
Try this: 尝试这个:
@using (Ajax.BeginForm("AddOption", "Manage",null, new AjaxOptions { OnSuccess = "handleSavedOptionChoice(data)", HttpMethod = "Post" }))
public ActionResult AddOptionChoice([System.Web.Http.FromBody]OptionViewModel model)
{
***content of function***
}
@{Html.RenderPartial("AddOptionChoicePartial", new OptionViewModel());}
passes exactly that; @{Html.RenderPartial("AddOptionChoicePartial", new OptionViewModel());}
传递的; a new, presumably empty, OptionViewModel
. 一个新的,大概是空的OptionViewModel
。
You should either pass in a pre-populated model (from the parent BuildViewModel
) like @{Html.RenderPartial("AddOptionChoicePartial", parentViewModel.SomeModel);}
or construct the View Model from scratch in AddOptionChoice
. 您应该传入一个预填充的模型(来自父BuildViewModel
),例如@{Html.RenderPartial("AddOptionChoicePartial", parentViewModel.SomeModel);}
或者在AddOptionChoice
从头构造视图模型。
(Note also that you're calling "AddOptionChoicePartial"
from Html.RenderPartial
, but the Controller Action is named AddOptionChoice
. They should match) (还要注意,您正在从Html.RenderPartial
调用"AddOptionChoicePartial"
,但Controller Action名为AddOptionChoice
。它们应该匹配)
Also, in trying to recreate the model from your comments, you're using ChoiceText and ChoiceValue, which do not match OptionText and OptionValue. 另外,在尝试根据注释重新创建模型时,您使用的是ChoiceText和ChoiceValue,它们与OptionText和OptionValue不匹配。
Error in the code found: 找到的代码错误:
@Html.LabelFor(m => m.ChoiceText)
@Html.TextBoxFor(m => m.ChoiceValue, new { @class = "form-control" })
@Html.RadioButtonFor(m => m.ChoiceValue, 1) True
Setting multiple values on a single parameter causes that parameter to act like a list, which only creates a ModelState error and will never fill the model. 在单个参数上设置多个值会使该参数像列表一样工作,这只会产生ModelState错误,并且永远不会填充模型。
To anyone who encounters a similar problem in the future: Debug your controller function and have a look at the ModelState in the debugger. 对于以后遇到类似问题的任何人:调试控制器功能并查看调试器中的ModelState。
Thanks to everyone who advised. 感谢所有提供建议的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.