mvc3提交模型为空 [英] mvc3 submit model empty

查看:88
本文介绍了mvc3提交模型为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我不了解的问题,而且似乎没有调试该问题的简便方法.我敢肯定这很简单.

I have a problem which I don't understand and there doesn't seem to be an easy way to debug the problem. I'm sure it's simple.

@model StartStop.ServiceResources.UserSettings

我的MVC3视图绑定了一个特定的模型;

my MVC3 view is bound a specific model;

 public class Setting
{
    public Int64 SettingID { get; set; }
    public Int64 UserID { get; set; }
    public int PreferenceType { get; set; }
    public string PreferenceName { get; set; }
    public bool PreferenceBool { get; set; }
    public int PreferenceInt { get; set; }
    public string PreferenceString { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime ModifiedOn { get; set; }

}

public class UserSettings
{
    public Int64 UserID { get; set; }
    public List<Setting> Settings { get; set; }
}

该视图列出了代表该列表的复选框;

the view lists out the check boxes which represent the list;

  @using (Html.BeginForm("ManageAccount","Account", FormMethod.Post))
        {
            <table class="tbl" cellspacing="0">

                <tr>
                    <th>Preference</th>
                    <th>Setting</th>
                </tr>

                @if (Model != null)
                {
                    foreach (var item in Model.Settings.ToList())
                    {
                        <tr>
                            <td>@item.PreferenceName
                            </td>
                            <td>
                                @if (item.PreferenceType == 2)
                                {
                                    @Html.CheckBoxFor(modelItem => item.PreferenceBool)
                                }
                            </td>
                        </tr>

                    }
                }
            </table>

            <input type="submit" value="Save Changes" class="action medium" />

        }

很好,我将数据加载到视图中以呈现视图并选择正确的设置.但是,当我在底部进行发布时,视图模型将返回null!我不确定为什么...

All good, I load the data into the view it renders the view and picks up the correct settings. However, when I do a post at the bottom, the view model returns a null! I'm not sure why...

 [HttpPost]
    [Authorize]
    public ActionResult ManageAccount(StartStop.ServiceResources.UserSettings model)
    {
        if (ModelState.IsValid)
        {

            foreach (StartStop.ServiceResources.Setting oSetting in model.Settings)
            {
                StartStop.Helpers.UserPreferences.SaveUserSetting(oSetting);
            }
        }
        return View(model); 
    }

任何人都可以帮忙吗?

Can anyone help?

推荐答案

问题出在您视图的以下行:

The problem is on the following line in your view:

@Html.CheckBoxFor(modelItem => item.PreferenceBool)

我看到人们经常在视图中编写以下lambda表达式modelItem => item.SomeProperty,并问为什么模型绑定器无法正确绑定其视图模型上的集合属性.

I see people writing the following lambda expression modelItem => item.SomeProperty in their views very often and asking why the model binder doesn't correctly bind collection properties on their view models.

这不会为复选框生成适当的名称,以便默认模型绑定程序能够重新创建Settings集合.建议您阅读后续博客文章,以更好地理解模型绑定程序期望的正确格式.

This won't generate proper name for the checkbox so that the default model binder is able to recreate the Settings collection. I would recommend you reading the following blog post to better understand the correct format that the model binder expects.

尝试这样:

@model StartStop.ServiceResources.UserSettings
@using (Html.BeginForm("ManageAccount", "Account", FormMethod.Post))
{
    <table class="tbl" cellspacing="0">
        <tr>
            <th>Preference</th>
            <th>Setting</th>
       </tr>

       @if (Model != null)
       {
           for (var i = 0; i < Model.Settings.Count; i++)
           {
               <tr>
                   <td>@Model.Settings[i].PreferenceName</td>
                   <td>
                       @if (Model.Settings[i].PreferenceType == 2)
                       {
                           @Html.CheckBoxFor(x => x.Settings[i].PreferenceBool)
                       }
                    </td>
                </tr>
           }
       }
    </table>

    <input type="submit" value="Save Changes" class="action medium" />
}

话虽如此,我建议您使用编辑器模板,如下所示:

This being said, I would recommend you using editor templates, like so:

@using (Html.BeginForm("ManageAccount","Account", FormMethod.Post))
{
    <table class="tbl" cellspacing="0">
        <tr>
            <th>Preference</th>
            <th>Setting</th>
       </tr>

       @if (Model != null)
       {
           @Html.EditorFor(x => x.Settings)
       }
    </table>

    <input type="submit" value="Save Changes" class="action medium" />
}

,然后定义一个自定义编辑器模板,该模板将自动为Settings集合(~/Views/Shared/EditorTemplates/Setting.cshtml)的每个元素呈现:

and then define a custom editor template which will automatcially be rendered for each element of the Settings collection (~/Views/Shared/EditorTemplates/Setting.cshtml):

@model StartStop.ServiceResources.Setting
<tr>
    <td>@Model.PreferenceName</td>
    <td>
         @if (Model.PreferenceType == 2)
         {
             @Html.CheckBoxFor(x => x.PreferenceBool)
         }
     </td>
 </tr>

此外,我在此表单中可以看到的唯一输入字段是复选框,该复选框绑定到模型上的PreferenceBool属性.因此,在您的POST控制器操作中,您将初始化设置"列表属性,但不要期望在此Setting类中为其他属性找到任何值,除非您当然在表单中包含了它们的输入字段(更确切地说,是在我显示的编辑器模板).

Also the only input field that I can see in this form is the checkbox which is bound to the PreferenceBool property on your model. So inside your POST controller action you will get the Settings list property initialized but don't expect to find any values for the other properties in this Setting class unless of course you include input fields for them in the form (and more precisely in the editor template that I have shown).

这篇关于mvc3提交模型为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆