Model view controller 在控制器和窗体之间切换时保持MVC的模型

Model view controller 在控制器和窗体之间切换时保持MVC的模型,model-view-controller,controller,models,Model View Controller,Controller,Models,所以我最近在.NETMVC中启动了这个项目,并且在保存数据时遇到了问题。我在某个地方读到,为了做到这一点,你必须来回传递模型。我试过这么做,但还是遇到了问题。在我的项目中,我现在有两个按钮,getData和getData2。我的视图打印它们的真/假值。当我按下一个时,它变为真,但如果我按下另一个,它变为真,但另一个变为假。我希望他们两个都保持真实,如果我对他们都施压的话 控制器: [HttpPost] public ActionResult GetData(TestSite.Models.Far

所以我最近在.NETMVC中启动了这个项目,并且在保存数据时遇到了问题。我在某个地方读到,为了做到这一点,你必须来回传递模型。我试过这么做,但还是遇到了问题。在我的项目中,我现在有两个按钮,getData和getData2。我的视图打印它们的真/假值。当我按下一个时,它变为真,但如果我按下另一个,它变为真,但另一个变为假。我希望他们两个都保持真实,如果我对他们都施压的话

控制器:

[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);
}
视图:

@使用(Html.BeginForm(“GetData”,“Home”,FormMethod.Post))
{
//@Html.TextBoxFor(model=>model.FarValue)
}
@使用(Html.BeginForm(“GetData2”,“Home”,FormMethod.Post))
{
//@Html.TextBoxFor(model=>model.FarValue)
}
@Model.HasData
@Model.HasData2

谢谢

您需要使用TempData对象,因为使用RedirectToAction返回状态代码302,并且模型绑定不存在。下面是一个很好的例子


您需要使用TempData对象,因为使用RedirectToAction返回状态代码302,并且模型绑定不存在。下面是一个很好的例子

@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