我的模型看起来像:
public class schoolModel
{
public SelectList Categories;
public school Entity;
}
学校的样子:
public class school
{
int? CategoryId{get;set;} // foreingKey
bool IncludeAttached{get;set;}
}
我按如下方式初始化schoolModel:
var model = new schoolModel();
model.Entity = schoolServiceAgent.Output.Entities.FirstOrDefault(n => n.Id == id);
model.Categories = new SelectList(new GenderServiceAgent().GetCategoriesByAlias(null, true), "Id", "Alias", model.Entity.CategoryId);
我就像
一样使用它们@Html.DropDownListFor(model => model.Entity.CategoryId, new SelectList(Model.Categories, "Value", "Text"), Model.Entity.CategoryId)
@Html.CheckBoxFor(model => model.Entity.IncludeAttached.Value, new { @class = "form-control placeholder-no-fix", name = "IncludeAttached", @checked = "checked" })
但是在编辑帖子中,FormCollection对CategoryId没有任何价值,而且IncludeAttached
我尝试使用
public ActionResult Edit(int id, FormCollection collection)
{
var categoryid = int.Parse(collection["CategoryId"]);
var result = selectedschool.inMediaArabicApp = bool.Parse(collection["IncludeAttached"]);
}
以上提出null异常,任何想法如何解决?编辑周围的表格看起来像
@using (@Html.BeginForm("Edit", "Schools", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
答案 0 :(得分:3)
而不是使用FormCollection为什么不使用.Net Model binding功能?
使用[HttpPost]
,因为您是从表单发布的。
[HttpPost]
public ActionResult Edit(School model)
{
var categoryid = model.CategoryId;
var result = model.IncludeAttached;
}
此外,c#约定是对类名和变量使用Pascal Casing。