2015-04-27 73 views
0
  • 我有一個視圖模型叫做ExampleViewModel
  • 我有一個控制器叫ExampleController
  • 我有兩個視圖叫Page1Page2

這兩個視圖都與ExampleViewModel強烈類型。如何在多個強類型視圖之間保留對象屬性值?

ExampleViewModel包含兩個屬性NameAge

  • Page1獲取屬性Name的數據。
  • Page2獲取屬性Age的數據。

當我訪問page1,我填的文本框爲Name財產,並觸及「下一步」按鈕,將數據上傳到控制器。

調試時可以看到發佈的數據正常。發送回發佈函數的數據是一個ExampleViewModel對象。然後我將同一個對象傳入Page2

當我然後訪問page2時,我填寫Age屬性的文本框並點擊Back按鈕,該按鈕將數據發佈到控制器。

這裏的問題,當你看看這回發的ExampleViewModel對象,這是在Page1設置Name屬性值失傳,只有Page2數據提交(Age屬性)。

如何保留在視圖之間傳遞的同一對象的屬性值?

ExampleViewModel

public class ExampleViewModel 
{ 
    public string Name { get; set; } 
    public int Age { get; set; } 
} 

ExampleController

public class ExampleController : Controller 
{ 
    [HttpPost] 
    [AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult PostMethod (ExampleViewModel formData, FormCollection form, string command) 
    { 
     string newView = ""; 
     string newController = ""; 

     Session["formData"] = formData; 

     if (command.ToLower() == "back") 
     { 
      newView = form["PreviousAction"]; 
      newController = form["PreviousController"]; 
     } 
     else if (command.ToLower() == "next") 
     { 
      newView = form["NextAction"]; 
      newController = form["NextController"]; 
     } 

     return RedirectToAction(newView, newController); 
    } 

    public ActionResult Page1() 
    { 
     return GetView(); 
    } 

    public ActionResult Page2() 
    { 
     return GetView(); 
    } 

    private ViewResult GetView() 
    { 
     ExampleViewModel formData = (ExampleViewModel)Session["formData"]; 

     if (formData != null) 
     { 
      return View(formData); 
     } 
     else 
     { 
      return View(); 
     } 
    } 
} 

1查看

@model ExampleProject.ViewModels.ExampleViewModel 

@using (Html.BeginForm("PostMethod", "Example", FormMethod.Post)) 
{ 
    @Html.Label("Your name") 
    @Html.TextBoxFor(m => m.Name) 

    <input id="ButtonNext" type="submit" name="Command" value="Next" /> 

    @Html.Hidden("nextaction", "page2") 
    @Html.Hidden("nextcontroller", "example") 
} 

第2頁查看

@model ExampleProject.ViewModels.ExampleViewModel 

@using (Html.BeginForm("PostMethod", "Example", FormMethod.Post)) 
{ 
    @Html.Label("Age") 
    @Html.TextBoxFor(m => m.Age) 

    <input id="ButtonBack" type="submit" name="Command" value="Back" /> 

    @Html.Hidden("previousaction", "page1") 
    @Html.Hidden("previouscontroller", "example") 
} 
+1

您的第2頁視圖不會爲'ExampleViewModel'的Name屬性提供輸入 –

+0

歡迎來到無狀態網頁...嘗試隱藏的表單值,會話或數據庫。 – BenjaminPaul

回答

2

這是因爲當你回來後第2頁,你不再有名稱設置爲它沒有任何地方依然存在。

堅持這一點的一種方式是,在Page2視圖中,還爲名稱屬性添加HiddenField,以便當您將Page2發回服務器時,名稱字段仍將被綁定。

@model ExampleProject.ViewModels.ExampleViewModel 

@using (Html.BeginForm("PostMethod", "Example", FormMethod.Post)) 
{ 
    @Html.Label("Age") 
    @Html.TextBoxFor(m => m.Age) 
    @Html.HiddenFor(m => m.Name) 

    <input id="ButtonBack" type="submit" name="Command" value="Back" /> 

    @Html.Hidden("previousaction", "page1") 
    @Html.Hidden("previouscontroller", "example") 
} 
0

Page1你應該通過一個隱藏的輸入Age值發回給控制器:

@using (Html.BeginForm("PostMethod", "Example", FormMethod.Post)) 
{ 
    @Html.Label("Your name") 
    @Html.TextBoxFor(m => m.Name) 
    @Html.HiddenFor(m => m.Age) 

    <input id="ButtonNext" type="submit" name="Command" value="Next" /> 

    @Html.Hidden("nextaction", "page2") 
    @Html.Hidden("nextcontroller", "example") 
} 

Page2做同樣的事情Name

@model ExampleProject.ViewModels.ExampleViewModel 

@using (Html.BeginForm("PostMethod", "Example", FormMethod.Post)) 
{ 
    @Html.Label("Age") 
    @Html.TextBoxFor(m => m.Age) 
    @Html.HiddenFor(m => m.Name) 

    <input id="ButtonBack" type="submit" name="Command" value="Back" /> 

    @Html.Hidden("previousaction", "page1") 
    @Html.Hidden("previouscontroller", "example") 
} 
0

正道做到這一點是

我可以想到兩個簡單的方法。

我)使用嚮導樣的客戶方的JavaScript控件(或jQuery的,Angularjs等),並在最後提交(步驟回傳)通過JavaScript 或 只顯示/隱藏在瀏覽器頁面的不同部分,然後提交(回傳)作爲最後一步。

下面是一個例子Link

OR

ii)除在每一個回發對象。

如果您要保存這些值,我的意思是在第1頁回發中保存'名稱',然後在第2頁回發您只需從數據庫中檢索保存的對象,並將'年齡'設置爲模型並將其保存回數據庫。

只更新允許用戶通過每個回發更新的模型的值。

我會不是建議保存會話中的這個對象。

相關問題