有很多关于这个问题的帖子,但我仍然找不到正确的解决方案。目前我的模型、视图和控制器如下所示:
看法:
@model Pro.Web.Models.CatDetailsView
@using (Html.BeginForm("Details", "Cat", Model, FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<h2>Details</h2>
<ol>
<li>
@Html.LabelFor(model => model.Cat.Name)
<p>@Html.DisplayFor(model => model.Cat.Name, new { @readonly = "true" })</p>
</li>
<li>
@Html.LabelFor(model => model.Cat.Owner)
<p>@Html.DisplayFor(model => model.Cat.Owner.UserName, new { @readonly = "true" })</p>
</li>
<li>
@Html.LabelFor(model => model.Cat.BornDate)
<p>@Html.DisplayFor(model => model.Cat.BornDate, new { @readonly = "true" })</p>
</li>
<li>
@Html.LabelFor(model => model.Cat.Age)
<p>@Html.DisplayFor(model => model.Cat.Age, new { @readonly = "true" })</p>
</li>
<li>
@Html.LabelFor(model => model.Cat.CatColorValue)
<p>@Html.DisplayEnumFor(model => model.Cat.CatColorValue, typeof(Pro.Web.Models.Entities.Enums.CatColor))</p>
</li>
<li>
@Html.LabelFor(model => model.Cat.Description)
<p>@Html.DisplayFor(model => model.Cat.Description, new { @readonly = "true" })</p>
</li>
</ol>
<input type="submit" value="Edit details" name="Command" />
}
模型:
public class CatDetailsView
{
public Cat Cat { get; set; }
}
控制器:
[HttpPost]
public ActionResult Details(CatDetailsView model, string Command)
{
if (Command.Equals("Edit details"))
{
Cat cat = this._catRepository.Find(model.Cat.Id);
if (cat != null)
{
return RedirectToAction("Edit", "Cats", new { id = cat.Id });
}
else
{
return View(model);
}
}
else
{
return View(model);
}
}
问题是控制器总是收到模型的空值。我正在查看该代码,但看不出有什么问题。我刚刚开始学习,所以我可能遗漏了一些微不足道的东西。我希望有人知道可能是什么问题。
谢谢你。