繁体   English   中英

MVC4-在视图之间传递模型

[英]MVC4 - Passing model between views

我有一个模型,我想要填写以下步骤:actionresult1(model)-> actionresult2(model)-actionresult3(model)

我的榜样是Person:

public class Person{
  string FirstName {get;set;}
  string Lastname {get;set;}
  int Age {get;set;}
}

在我的PersonController中,我有三个ActionResult:

public ActionResult FillFirstName(Person model)//First page where i start. Model is empty
    {
            return View("~/Views/FillFirstName.cshtml", model);           
    } 
public ActionResult FillLastName(Person model)//Second page, where first name is filled
    {
            return View("~/Views/FillLastName.cshtml", model);           
    } 
public ActionResult FillAge(Person model)//When i click submit button in FillLastName.cshtml view then it submits form here and model have filled only LastName and FirstName is empty.
    {
            return View("~/Views/FillAge.cshtml", model);           
    } 

我的三个观点是:

1)FillFirstName.cshtml

@using (@Html.BeginForm("FillLastName", "Person"))
{
   @Html.TextBoxFor(m => m.FirstName)
   <input type="submit" name="Next" value="Next" />
}

2)FillLastName.cshtml

@using (@Html.BeginForm("FillAge", "Person"))
{
   @Html.TextBoxFor(m => m.LastName)
   <input type="submit" name="Next" value="Next" />
}

3)FillAge.cshtml

@using (@Html.BeginForm("NextAction", "Person"))
{
   @Html.TextBoxFor(m => m.Age)
   <input type="submit" name="Next" value="Next" />
}

问题:当我尝试在视图之间传递模型时,它包含所有我上次视图提交的数据。

原因:我的表格是2000行,我想将其切成小块。

有什么办法可以使用Viewbag或ModelState或其他方法使模型填充我在前一页提交的所有数据? 有人可以给我例子吗? :)

HTTP是无状态的-模型只能与当前请求中的内容绑定。 因此,要访问上一个控制器操作中的所有内容,您需要确保所有内容均在请求的表单中发送。 使用隐藏字段可在多个视图上保留数据:

FillLastName:

@using (@Html.BeginForm("FillAge", "Person"))
{
   @Html.HiddenFor(m => m.FirstName)
   @Html.TextBoxFor(m => m.LastName)
   <input type="submit" name="Next" value="Next" />
}

填充率:

@using (@Html.BeginForm("NextAction", "Person"))
{
   @Html.HiddenFor(m => m.FirstName)
   @Html.HiddenFor(m => m.LastName)
   @Html.TextBoxFor(m => m.Age)
   <input type="submit" name="Next" value="Next" />
}

与使用“伪状态”机制(如会话状态)相比,这是一种在多个请求上持久保存表单数据的更干净,更常规的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM