我有一个视图模型
public class DeviceModelEntryViewModel
{
public int ID { get; set; }
[Required]
[MaxLength(255)]
public String Model { get; set; }
[MaxLength(255)]
public String Description { get; set; }
[Required]
[Display(Name = "Manufacturer")]
public int ManufacturerID { get; set; }
public SelectList ManufacturerList { get; set; }
}
在DeviceModelController
中我的Create方法是这样的:
[HttpPost]
public ActionResult Create(DeviceModelEntryViewModel model)
{
if (ModelState.IsValid)
{
...
return RedirectToAction("Edit", new { id });
}
return View(model);
}
创建视图如下:
@model ICMDB.ViewModels.DeviceModelEntryViewModel
@{ ViewBag.Title = "ICMD - Create Device Model"; }
<h2>Create Device Model</h2>
<div class="form-div">
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<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.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ManufacturerID)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.ManufacturerID,
Model.ManufacturerList, "--None--")
@Html.ValidationMessageFor(model => model.ManufacturerID)
</div>
</fieldset>
<input type="submit" value="Save" class="form-button"/>
}
@using (Html.BeginForm("Index", "DeviceModel"))
{ <input type="submit" value="Cancel" class="form-button"/> }
</div>
出于某种原因,只要命中此函数(或具有类似声明的Edit函数),model
为空。我查了一下,它甚至没有点击DeviceModelEntryViewModel
的零arg构造函数。我已经为所有其他控制器使用适当的视图模型完成了这项工作,并且它们都能正常工作。我似乎无法看到阻止它工作的那个区别。
有什么想法吗?
答案 0 :(得分:1)
尝试将Model属性的名称更改为:
public String ModelName { get; set; }
不要忘记更新您的观点。我认为应该让它正常工作,因为模型这个词看起来像一个保留字并且混淆了模型绑定器。
编辑:以下是Razor / MVC中保留字的列表:Razor reseverd words