【问题标题】:ASP.NET MVC Model Binder returns null objectASP.NET MVC 模型绑定器返回空对象
【发布时间】:2011-11-22 11:02:57
【问题描述】:

我遇到一个问题,每次我将表单发回控制器操作的[HttpPost] 版本时,ModelBinder 都会返回一个空对象。我不知道为什么。如果我将签名更改为使用FormCollection,我可以看到所有正确的密钥都已设置。谁能帮我指出这里出了什么问题,因为我无法发现它。

以下是处理我的视图的模型

public class DeviceModel
{
    public int Id { get; set; }

    [Required]
    [Display(Name = "Manufacturer")]
    public int ManufacturerId { get; set; }

    [Required]
    [Display(Name = "Model")]
    [StringLength(20)]
    public string Model { get; set; }

    [StringLength(50)]
    [Display(Name = "Name")]
    public string Name { get; set; }

    [StringLength(50)]
    [Display(Name = "CodeName")]
    public string CodeName { get; set; }

    public int? ImageId { get; set; }
}

public class DeviceCreateViewModel : DeviceModel
{
    public IEnumerable<SelectListItem> Manufacturers { get; set; } 
}

我在控制器中这样使用:

public ActionResult Create()
{
    DeviceCreateViewModel viewModel = new DeviceCreateViewModel()
                                            {
                                                Manufacturers = ManufacturerHelper.GetSortedManufacturersDropDownList()
                                            };

    return View(viewModel);
}

[HttpPost]
public ActionResult Create(DeviceModel model)
{
    // if I check model here it is NULL
    return View();
}

视图看起来像这样:

@model TMDM.Models.DeviceCreateViewModel

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>

        <div class="editor-label">
            @Html.LabelFor(model => model.ManufacturerId)
        </div>
        <div class="editor-field">
            @Html.DropDownList("ManufacturerId", Model.Manufacturers)
            @Html.ValidationMessageFor(model => model.ManufacturerId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Model)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Model)
            @Html.ValidationMessageFor(model => model.Model)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CodeName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CodeName)
            @Html.ValidationMessageFor(model => model.CodeName)
        </div>

        <p>
            <input type="submit" value="Save" class="medium green awesome" />
            @Html.ActionLink("Cancel", "Index", "Device", null, new { @class="medium black awesome" })
        </p>
    </fieldset> }

【问题讨论】:

    标签: asp.net asp.net-mvc-3 model-binding


    【解决方案1】:

    问题是DeviceModel 类中名为Model 的属性与Create 操作中名为model 的变量之间存在名称冲突。名称冲突导致 DefaultModelBinder 失败,因为它试图将 Model 属性绑定到 DeviceModel 类。

    将 Create 操作中的变量名称更改为 deviceModel,它将正确绑定。

    【讨论】:

    • 成功了,谢谢!但是为什么名称模型适用于我拥有的另一个接受 HttpPost 数据的控制器?
    • 该操作的模型是否也有一个名为 Model 的属性?它在这里失败的原因是名称冲突。
    • 哦,对不起,我实际上误解了你之前所说的,但我现在清楚了。谢谢你的帮助。
    • 我也遇到了同样的问题,谢谢解答。这并不明显,真的。
    • 我遇到了类似的问题,但在我的情况下,参数名称是“响应”,因为我正在收集 R.S.V.P 的 - 响应是一个合理的名称,而不是保留字。在这种情况下,我担心配置口头上的约定似乎太过分了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 2015-03-28
    • 2013-04-02
    相关资源
    最近更新 更多