我想将模型传递给控制器的 Post 方法。调用方法时,它显示内容为空值,ID 为 0。理想情况下,它应该包含它显示的模型的值。
看法:
@model MvcApplication4.Models.WorldModel
@{
ViewBag.Title = "Information";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("Information", "World", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<br />
@Html.DisplayFor(model => model.Id)
<br />
@Html.DisplayFor(model => model.Content)
<br />
<input type="submit" value="Next" />
}
控制器:单击提交按钮时调用的方法。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Information(WorldModel worldModel)
{
CandidateSession cs = (CandidateSession)Session["Can"];
var can = cs.Candidates.Where(x => x.IsNameDispalyed == false);
if (can.Count() > 0)
{
var can1 = can.First();
can1.IsNameDispalyed = true;
Session["Can"] = cs;
return View(new WorldModel() { Id = can1.Id, Content = can1.Name });
}
return View(new WorldModel());
}
模型:
public class WorldModel
{
public int Id { get; set; }
public string Content { get; set; }
}