提交表单时,空模型在 MVC 中传递给控制器
Null model passed in MVC to controller when form is submitted
我正在使用 MVC4 应用程序,并且有一个控制器允许用户输入一些数据。问题是我在从表单返回的所有属性中都得到了空值。我正在使用强类型模型,并且我没有看到任何名称冲突,正如少数 post 中关于传递空模型所建议的那样。
这是我的模特
public class NewsItemView
{
[Required]
public string NewsTitle;
public string NewsAuthor;
public string NewsSummary;
public string NewsDescription;
public bool NewsAllowComments;
[Required]
public string NewsContent;
public string NewsSourceName;
}
控制器操作(获取)
[HttpGet]
public ActionResult Create()
{
NewsItemView nv = new NewsItemView();
return View(nv);
}
查看:
@model NewsItemView
@using (Html.BeginForm("Create", "Service", FormMethod.Post, new { @id = "NewsForm" }))
{
@Html.LabelFor(model => model.NewsTitle)
@Html.EditorFor(model => model.NewsTitle)
<br />
@Html.LabelFor(model => model.NewsAuthor)
@Html.EditorFor(model => model.NewsAuthor)
<br />
@Html.LabelFor(model => model.NewsAllowComments)
@Html.EditorFor(model => model.NewsAllowComments)
<br />
@Html.LabelFor(model => model.NewsSummary)
@Html.EditorFor(model => model.NewsSummary)
<br />
@Html.LabelFor(model => model.NewsDescription)
@Html.EditorFor(model => model.NewsDescription)
<br />
@Html.LabelFor(model => model.NewsContent)
@Html.EditorFor(model => model.NewsContent)
<br />
@Html.LabelFor(model => model.NewsSourceName)
@Html.EditorFor(model => model.NewsSourceName)
<br />
<button type="submit" id="submitBtn">Create News</button>
}
控制器操作(post)
[HttpPost]
public ActionResult Create(NewsItemView newsModel)
{
//my code here
}
如果我使用 FormsCollection,我可以使用索引提取值,知道为什么强类型模型不起作用吗?
我已经在我的环境中重现了这个问题,我为修复它所做的一切只是将模型字段更新为如下属性
public class NewsItemViewModel
{
[Required]
public string NewsTitle { get; set; }
public string NewsAuthor { get; set; }
public string NewsSummary { get; set; }
public string NewsDescription { get; set; }
public bool NewsAllowComments { get; set; }
[Required]
public string NewsContent { get; set; }
public string NewsSourceName { get; set; }
}
我正在使用 MVC4 应用程序,并且有一个控制器允许用户输入一些数据。问题是我在从表单返回的所有属性中都得到了空值。我正在使用强类型模型,并且我没有看到任何名称冲突,正如少数 post 中关于传递空模型所建议的那样。 这是我的模特
public class NewsItemView
{
[Required]
public string NewsTitle;
public string NewsAuthor;
public string NewsSummary;
public string NewsDescription;
public bool NewsAllowComments;
[Required]
public string NewsContent;
public string NewsSourceName;
}
控制器操作(获取)
[HttpGet]
public ActionResult Create()
{
NewsItemView nv = new NewsItemView();
return View(nv);
}
查看:
@model NewsItemView
@using (Html.BeginForm("Create", "Service", FormMethod.Post, new { @id = "NewsForm" }))
{
@Html.LabelFor(model => model.NewsTitle)
@Html.EditorFor(model => model.NewsTitle)
<br />
@Html.LabelFor(model => model.NewsAuthor)
@Html.EditorFor(model => model.NewsAuthor)
<br />
@Html.LabelFor(model => model.NewsAllowComments)
@Html.EditorFor(model => model.NewsAllowComments)
<br />
@Html.LabelFor(model => model.NewsSummary)
@Html.EditorFor(model => model.NewsSummary)
<br />
@Html.LabelFor(model => model.NewsDescription)
@Html.EditorFor(model => model.NewsDescription)
<br />
@Html.LabelFor(model => model.NewsContent)
@Html.EditorFor(model => model.NewsContent)
<br />
@Html.LabelFor(model => model.NewsSourceName)
@Html.EditorFor(model => model.NewsSourceName)
<br />
<button type="submit" id="submitBtn">Create News</button>
}
控制器操作(post)
[HttpPost]
public ActionResult Create(NewsItemView newsModel)
{
//my code here
}
如果我使用 FormsCollection,我可以使用索引提取值,知道为什么强类型模型不起作用吗?
我已经在我的环境中重现了这个问题,我为修复它所做的一切只是将模型字段更新为如下属性
public class NewsItemViewModel
{
[Required]
public string NewsTitle { get; set; }
public string NewsAuthor { get; set; }
public string NewsSummary { get; set; }
public string NewsDescription { get; set; }
public bool NewsAllowComments { get; set; }
[Required]
public string NewsContent { get; set; }
public string NewsSourceName { get; set; }
}