因此,我最近在.net MVC中启动了一个项目,在保存数据时遇到了一些问题。我在某处读到,为了做到这一点,你必须来回传递模型。我试过这样做,但仍然遇到问题。在我的项目中,我现在有两个按钮,getData和getData2。我的视图打印它们的true/false值。当我按下其中一个,它变成真,但如果我按下另一个,它变成真,而另一个变成假。如果我同时按下它们,我希望它们都保持正确。
控制器:
[HttpPost]
public ActionResult GetData(TestSite.Models.FarModels theFars)
{
theFars.HasData = true;
return RedirectToAction("FarCalc",theFars);
}
[HttpPost]
public ActionResult GetData2(TestSite.Models.FarModels theFars)
{
theFars.HasData2 = true;
return RedirectToAction("FarCalc", theFars);
}
[HttpGet]
public ActionResult FarCalc(TestSite.Models.FarModels theFars)
{
return View(theFars);
}
查看:
@using (Html.BeginForm("GetData", "Home", FormMethod.Post))
{
//@Html.TextBoxFor(model => model.FarValue)
<input type="submit" value="GetData" />
}
@using (Html.BeginForm("GetData2", "Home", FormMethod.Post))
{
//@Html.TextBoxFor(model => model.FarValue)
<input type="submit" value="GetData2" />
}
@Model.HasData
@Model.HasData2
谢谢
发布于 2017-01-06 22:00:43
您需要使用TempData对象,因为使用RedirectToAction会返回状态代码302,而模型绑定并不存在。下面是一个很好的例子。
https://www.codeproject.com/Tips/842080/How-to-Persist-Data-with-TempData-in-MVC
https://stackoverflow.com/questions/41513907
复制