2014-03-27 202 views
2

我使用的是ViewBag爲我檢查點partial view我應該呈現在某個div來這,這裏是我的代碼:ASP.Net MVC4剃刀ViewBag

在控制器:

[HttpPost] 
    public ActionResult NewAppointment(appointment.AppointmentInformation model) 
    { 
     if (ViewBag.NextForm == null || ViewBag.NextForm == "undefined") 
     { 
      info.CustomerType = model.CustomerType; 
      ViewBag.NextForm = "Information"; 
     } 
     else if (ViewBag.NextForm == "Information") 
     { 
      info.CustomerID = String.IsNullOrEmpty(model.CustomerID) ? "" : model.CustomerID; 
      info.CustomerName = String.IsNullOrEmpty(model.CustomerName) ? "" : model.CustomerName; 
      info.CustomerCNum = String.IsNullOrEmpty(model.CustomerCNum) ? "" : model.CustomerCNum; 
      ViewBag.NextForm = "Services"; 
     } 
     else if (ViewBag.NextForm == "Services") 
     { 
      //do nothing; 
     } 
     return View(info); 
    } 

在查看:

<form method="post"> 
<div id="PartialContainer"> 

    @if (ViewBag.NextForm == null || ViewBag.NextForm == "undefined") 
    { 
     @Html.Partial("CustomerType") 
    } 
    @if (ViewBag.NextForm == "Information") 
    { 
     @Html.Partial("GuestInformation") 
    } 
    @if (ViewBag.NextForm == "Services") 
    { 
     @Html.Partial("Service") 
    } 

</div> 
<div id="ButtonArea"> 
    <button id="btnCancel">Cancel</button> 
    <button id="btnBack">Back</button> 
    <button id="btnNext">Next</button> 
</div> 

第三次點擊btnNext,@ Html.Part @Html.Partial("Service")不起作用。但前兩個@Html.Partial工作正常..

我的代碼有什麼問題?

回答

1

你爲什麼不嵌入表單中的步驟(在你的諧音)?

這樣,無論何時提交表單,您都會得到它的哪一步,並從控制器代碼顯示正確的下一步,而不是視圖。

您可以將其添加爲您的ViewModel的屬性,或者只是簡單地發佈它並從您的Controller中的Request對象中獲取它。

僞代碼:

[HttpPost] 
    public ActionResult NewAppointment(appointment.AppointmentInformation model) 
    { 
     //get the current step or start with empty string 
     //although if this is the POST method, you should have the 
     //first value, set in your GET method to show the form! 
     var step = Request.Params["NextForm"] ?? ""; 

     if (step == "") 
     { 
      info.CustomerType = model.CustomerType; 
      ViewBag.NextForm = "Information"; 
     } 
     else if (step == "Information") 
     { 
      info.CustomerID = String.IsNullOrEmpty(model.CustomerID) ? "" : model.CustomerID; 
      info.CustomerName = String.IsNullOrEmpty(model.CustomerName) ? "" : model.CustomerName; 
      info.CustomerCNum = String.IsNullOrEmpty(model.CustomerCNum) ? "" : model.CustomerCNum; 
      ViewBag.NextForm = "Services"; 
     } 
     else if (step == "Services") 
     { 
      //do nothing; 
     } 
     return View(info); 
    } 

,然後使用您認爲值將它與你的形式發送的任何時候。

<form method="post"> 
<div id="PartialContainer"> 
    <input type="hidden" name="NextForm" value="@(ViewBag.NextForm ?? "")" /> 

    @if (ViewBag.NextForm == null || ViewBag.NextForm == "undefined") 
    { 
     @Html.Partial("CustomerType") 
    } 
    @if (ViewBag.NextForm == "Information") 
    { 
     @Html.Partial("GuestInformation") 
    } 
    @if (ViewBag.NextForm == "Services") 
    { 
     @Html.Partial("Service") 
    } 

</div> 
<div id="ButtonArea"> 
    <button id="btnCancel">Cancel</button> 
    <button id="btnBack">Back</button> 
    <button id="btnNext">Next</button> 
</div> 

您將獲得NextStep自動與您的形式每次,然後你只需要使用ViewBag從控制器傳遞數據進行查看,這是預期的用途。

希望它對你有意義。

+0

這個方法真的幫了我很多。 –

0

HTTP is a stateless protocol。這意味着每個請求都是新的,客戶端和服務器之間的任何以前的交互大多是「被遺忘」的。

每次用戶發出請求時,ViewBag都會從頭開始重新初始化。在我看來,您的代碼假設ViewBag在客戶端的瀏覽器中「掛起」,直到他們發出下一個請求。不幸的是事情不這樣。

ViewBag.NextForm等於"Services"永遠不會發生,因爲應用程序不知道用戶在哪裏。

在整個HTTP應用程序中維護狀態是一個長期和持續的討論:How can I keep "state" information between calls to my CGI program?(1996?)。

沒有一個明顯的解決方案,我可以給你。一種方法是將表單中的信息保存爲隱藏輸入和POST數據返回到您的控制器,以便它知道用戶在哪裏。您也可以在cookies會話中保存信息。

+0

你好羅文先生,使用會話/ cookies是我的最後一招,儘可能的,我真的不想使用會話/ cookies由於機密信息。 –

0

你爲什麼不使用AJAX這個

@using (Ajax.BeginForm("Action", "Controller", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "PartialContainer" })) 
{ 
} 

UpdateTargetId =「PartialContainer」將更新與局部視圖的DIV,你必須返回來自控制器的局部視圖

+0

其實,我是新來的MVC4剃鬚刀,我真的不知道如何使用Ajax.BeginForm,但我會嘗試它。謝謝 –

+0

@JohnPauloCastro,然後檢查這個視頻,這將幫助你http://www.youtube.com/watch?v=B59skvlroyc – Golda

+0

你好Golda,它不工作。 –

0

是的,確實HTTP無狀態協議

您的值將在請求之間丟失。

ASP.Net webforms使用ViewState來解決此問題。

ASP.Net MVC不支持ViewState。

爲什麼不把你的數據放在會話?否則,你可以讓Ajax調用防止entire page refresh

+0

由於機密數據,我無法使用會話,而且我不善於處理會話,並且對於ajax調用,我嘗試了但ViewBag.nextForm的值保持不變。 –