ASP.NET MVC模型绑定器返回空对象

ASP.NET MVC模型绑定器返回空对象,asp.net,asp.net-mvc-3,model-binding,Asp.net,Asp.net Mvc 3,Model Binding,我遇到一个问题,每次我将表单发回控制器操作的[HttpPost]版本时,ModelBinder都会返回一个空对象。我不明白为什么。如果我将签名改为使用FormCollection,我可以看到所有正确的键都已设置。有人能帮我指出这里出了什么问题吗,因为我看不出来 以下是使用我的视图的模型 public class DeviceModel { public int Id { get; set; } [Required] [Display(Name = "Manufactur

我遇到一个问题,每次我将表单发回控制器操作的
[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; } 
}
视图如下所示:

@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> }
@model TMDM.Models.DeviceCreateViewModel
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
@LabelFor(model=>model.ManufacturerId)
@DropDownList(“ManufacturerId”,Model.Manufacturers)
@Html.ValidationMessageFor(model=>model.ManufacturerId)
@LabelFor(model=>model.model)
@EditorFor(model=>model.model)
@Html.ValidationMessageFor(model=>model.model)
@LabelFor(model=>model.Name)
@EditorFor(model=>model.Name)
@Html.ValidationMessageFor(model=>model.Name)
@LabelFor(model=>model.CodeName)
@EditorFor(model=>model.CodeName)
@Html.ValidationMessageFor(model=>model.CodeName)

@ActionLink(“取消”,“索引”,“设备”,空,新的{@class=“中黑色”})

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


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

成功了,谢谢!但是为什么名称模型适用于我拥有的另一个接受HttpPost数据的控制器?该操作的模型是否也有一个名为model的属性?这里失败的原因是因为名字冲突。哦,对不起,我实际上误解了你之前说的话,但我现在清楚了。谢谢你的帮助。我也有同样的问题,谢谢你的回答。这不是很明显,真的。我有一个类似的问题,但在我的例子中,参数名是'response',因为我正在收集R.S.V.P的-response是一个合理的名称,而不是保留字。这确实让我担心,在这种情况下,关于配置的约定似乎有些过头了
@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> }