【发布时间】:2015-06-05 16:57:11
【问题描述】:
我对 MVC 还很陌生,在 POST 页面上持久保存值时遇到了问题。下面是我正在尝试做的简化版本。基本上我有一个显示用户名和其他详细信息的表单。在 POST ActionResult 上,如果业务规则失败,则页面应返回到当前视图,否则将继续,但在我的 POST 上,我在 GET 中设置的名称和地址不会出现在视图模型的 POST 中,导致发送空值返回返回视图(模型)。有什么方法可以保留这些值而无需返回数据库来检索它们?
型号
public class HomeModel
{
public string Forename { get; set; }
public string Surname { get; set; }
public string AddressLine1 { get; set; }
public bool Continue { get; set; }
}
控制器
[HttpGet]
public ActionResult Index()
{
var model = new HomeModel();
model.Forename = "Joe";
model.Surname = "Boe";
model.AddressLine1 = "Unknown";
return View(model);
}
[HttpPost]
public ActionResult Index(HomeModel homeModel)
{
if (homeModel.Continue)
return RedirectToAction("Index", "Form2");
else
return View(homeModel);
}
查看
@using (Html.BeginForm())
{
@Html.LabelFor(m => m.Forename, Model.Forename)<br />
@Html.LabelFor(m => m.Surname, Model.Surname)<br />
@Html.LabelFor(m => m.AddressLine1, Model.AddressLine1)<br /><br />
@Html.CheckBoxFor(m => m.Continue)<br /><br />
<input type="submit" value="Click" />
}
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-4