ASP.NET MVC模型绑定返回null对象 [英] ASP.NET MVC Model Binder returns null object
问题描述
我有在那里每次我张贴的形式回 [HttpPost]
版本我控制器操作的问题,ModelBinder的返回一个空的对象。我不明白为什么。如果我更改签名使用的FormCollection
,而不是我能看到所有正确的键都被设置。有人可以帮助我针点什么错在这里,因为我无法发现它。
下面是型号为我的看法工作
公共类DeviceModel
{
公众诠释标识{搞定;组; } [需要]
[显示(名称=制造商)]
公众诠释ManufacturerId {搞定;组; } [需要]
[显示(NAME =模型)
[StringLength(20)]
公共字符串模式{搞定;组; } [StringLength(50)]
[显示(名称=名称)]
公共字符串名称{;组; } [StringLength(50)]
[显示(NAME =codeNAME)]
公共字符串$ C $ {CNAME获得;组; } 公众诠释? ImageId {搞定;组; }
}公共类DeviceCreateViewModel:DeviceModel
{
公共IEnumerable的< SelectListItem>制造商{搞定;组; }
}
我在我的控制器使用像这样:
公众的ActionResult的Create()
{
DeviceCreateViewModel视图模型=新DeviceCreateViewModel()
{
制造商= ManufacturerHelper.GetSortedManufacturersDropDownList()
}; 返回视图(视图模型);
}[HttpPost]
公众的ActionResult创建(DeviceModel模型)
{
//如果我在这里检查模式为NULL
返回查看();
}
和的观点是这样的:
@model TMDM.Models.DeviceCreateViewModel<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.min.js)TYPE =文/ JavaScript的>< / SCRIPT> <脚本的src =@ Url.Content(〜/脚本/ jquery.validate.unobtrusive.min.js)TYPE =文/ JavaScript的>< / SCRIPT>@using(Html.BeginForm()){
@ Html.ValidationSummary(真)
<&字段集GT; < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.ManufacturerId)
< / DIV>
< DIV CLASS =主编场>
@ Html.DropDownList(ManufacturerId,Model.Manufacturers)
@ Html.ValidationMessageFor(型号=> model.ManufacturerId)
< / DIV> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Model)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Model)
@ Html.ValidationMessageFor(型号=> model.Model)
< / DIV> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Name)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Name)
@ Html.ValidationMessageFor(型号=> model.Name)
< / DIV> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=方式>模式codeNAME)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=方式>模式codeNAME)
@ Html.ValidationMessageFor(型号=方式>模式codeNAME)
< / DIV> &所述p为H.;
<输入类型=提交值=保存级=中的绿色真棒/>
@ Html.ActionLink(取消,索引,设备,空,新的{@类=中等黑真棒})
&所述; / P>
< /字段集> }
的问题是,有一个在类命名的属性模式
之间的名称冲突 DeviceModel
并命名为模式
在创建
操作变量。该名冲突导致 DefaultModelBinder
失败,因为它试图将Model属性绑定到DeviceModel类。
更改创建行动变量的名称 deviceModel
键,它将正确绑定。
I am having a problem where everytime I post a form back to the [HttpPost]
version of my controller action, the ModelBinder returns a null object. I can't work out why. If I change the signature to use a FormCollection
instead I can see that all the correct keys have been set. Can someone help me pin point what's wrong here, because I can't spot it.
Here are the models for working with my views
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; }
}
Which I use in my controller like so:
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();
}
And the view looks like this:
@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> }
The problem is that there is a name collision between the property named Model
in the class DeviceModel
and the variable named model
in the Create
action. The name collision causes the DefaultModelBinder
to fail, since it tries to bind the Model property to the DeviceModel class.
Change the name of the variable in the Create action to deviceModel
and it will bind correctly.
这篇关于ASP.NET MVC模型绑定返回null对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!