C# 未在POST函数调用中创建ViewModel
我有一个视图模型C# 未在POST函数调用中创建ViewModel,c#,.net,asp.net-mvc,C#,.net,Asp.net Mvc,我有一个视图模型 public class DeviceModelEntryViewModel { public int ID { get; set; } [Required] [MaxLength(255)] public String Model { get; set; } [MaxLength(255)] public String Description { get; set; } [Required] [Displa
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
中,我的创建方法如下所示:
[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>
@model ICMDB.ViewModels.DeviceModelEntryViewModel
@{ViewBag.Title=“ICMD-创建设备模型”;}
创建设备模型
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
@LabelFor(model=>model.model)
@EditorFor(model=>model.model)
@Html.ValidationMessageFor(model=>model.model)
@LabelFor(model=>model.Description)
@EditorFor(model=>model.Description)
@Html.ValidationMessageFor(model=>model.Description)
@LabelFor(model=>model.ManufacturerID)
@Html.DropDownListFor(model=>model.ManufacturerID,
Model.ManufacturerList,“--None--”)
@Html.ValidationMessageFor(model=>model.ManufacturerID)
}
@使用(Html.BeginForm(“索引”、“设备模型”))
{ }
出于某种原因,每当点击此函数(或具有类似声明的编辑函数)时,model
为空。我检查过了,它甚至没有命中DeviceModelEntryViewModel
的零参数构造函数。我已经为所有其他具有适当视图模型的控制器执行了此操作,它们都工作正常。我似乎看不出这台机器有什么不同,它停止工作了
有什么想法吗?尝试将模型属性的名称更改为:
公共字符串ModelName{get;set;}
不要忘记更新您的视图。我认为应该让它正常工作,因为单词模型看起来像一个保留单词,并且混淆了模型绑定器
编辑:以下是Razor/MVC中的保留字列表:尝试将模型属性的名称更改为类似以下内容:
公共字符串ModelName{get;set;}
不要忘记更新您的视图。我认为应该让它正常工作,因为单词模型看起来像一个保留单词,并且混淆了模型绑定器
编辑:这里是Razor/MVC中保留字的列表:默认型号活页夹出现故障。原因有很多。很抱歉响应太晚,但基本上检查ViewData.ModelState.IsValid,如果在ModelState键/值对集合中查找为false,则存在ErrorMessage/Exception类。我有一个扩展方法,将所有绑定错误转储到调试控制台。默认的模型绑定失败。原因有很多。很抱歉响应太晚,但基本上检查ViewData.ModelState.IsValid,如果在ModelState键/值对集合中查找为false,则存在ErrorMessage/Exception类。我有一个扩展方法,将所有绑定错误转储到调试控制台。