简体   繁体   English

ASP.NET MVC-模型未在部分视图中绑定到Ajax.BeginForm

[英]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.

相关问题 Asp.Net MVC和部分视图以及混合Ajax.BeginForm和Html.BeginForm - Asp.Net MVC and Partial Views and Mixing Ajax.BeginForm and Html.BeginForm ASP.NET MVC 5-具有空参数的ajax.beginform() - ASP.NET MVC 5 - ajax.beginform() with null parameters asp.net mvc Ajax.BeginForm复制元素 - asp.net mvc Ajax.BeginForm duplicating elements 带有ASP.NET MVC 4的Ajax.BeginForm没有调用控制器动作 - Ajax.BeginForm with ASP.NET MVC 4 not calling controller action Asp.net MVC 4 Ajax.BeginForm提交+ mvc.grid-页面重新加载 - Asp.net MVC 4 Ajax.BeginForm submit + mvc.grid - page reloading ELMAH不记录从ASP.NET MVC Ajax.BeginForm引发的异常 - ELMAH doesn't log exceptions raised from ASP.NET MVC Ajax.BeginForm Ajax.BeginForm 向控制器操作 ASP.NET MVC 发送空值 - Ajax.BeginForm sends null values to controller action ASP.NET MVC ASP.NET MVC:如何在页面加载后更改Ajax.BeginForm将发布到的位置? - ASP.NET MVC: How do I change where Ajax.BeginForm will post to after the page loads? asp.net mvc 项目在使用 Ajax.BeginForm 时不显示带有自定义注释的错误消息 - asp.net mvc project does not display error message with custom annotation when using Ajax.BeginForm 在Ajax.BeginForm中使用图像提交标记的ASP.NET MVC问题 - ASP.NET MVC problem using image submit tag in Ajax.BeginForm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM