简体   繁体   English

MVC4-在视图之间传递模型

[英]MVC4 - Passing model between views

I have a model what i want fill in steps like this: actionresult1(model)->actionresult2(model)-actionresult3(model) 我有一个模型,我想要填写以下步骤:actionresult1(model)-> actionresult2(model)-actionresult3(model)

My model for exaple is Person: 我的榜样是Person:

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

In my PersonController i have three ActionResults: 在我的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);           
    } 

And my three views are: 我的三个观点是:

1)FillFirstName.cshtml 1)FillFirstName.cshtml

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

2)FillLastName.cshtml 2)FillLastName.cshtml

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

3)FillAge.cshtml 3)FillAge.cshtml

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

Problem: When i try to pass model between views it contains olny this data what i have submited on last view. 问题:当我尝试在视图之间传递模型时,它包含所有我上次视图提交的数据。

Reason: I have form what is 2000 lines, and i want to cut it into smaller pieces. 原因:我的表格是2000行,我想将其切成小块。

Is there someway i can use Viewbag or ModelState or something to keep the model filled with all data what i have submitted on previous pages? 有什么办法可以使用Viewbag或ModelState或其他方法使模型填充我在前一页提交的所有数据? Can someone give me some example please? 有人可以给我例子吗? :) :)

HTTP is stateless - the model can only be bound from what's in the current request. HTTP是无状态的-模型只能与当前请求中的内容绑定。 Therefore, to have access to everything in your last controller action, you need to make sure everything is sent in the request's form post. 因此,要访问上一个控制器操作中的所有内容,您需要确保所有内容均在请求的表单中发送。 Use hidden fields to persist data over multiple views: 使用隐藏字段可在多个视图上保留数据:

FillLastName: FillLastName:

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

FillAge: 填充率:

@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" />
}

This is a cleaner and more conventional way of persisting form data over multiple requests than using "pseudo-state" mechanics like session state. 与使用“伪状态”机制(如会话状态)相比,这是一种在多个请求上持久保存表单数据的更干净,更常规的方式。

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

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