2012-04-02 32 views

回答

3

,如果您使用的是Html.BeginForm像這樣將發生的職務:

<% using(Html.BeginForm("HandleForm", "Home")) { %> 
    <fieldset> 
     <legend>Fields</legend> 
     <p> 
      <%= Html.TextBoxFor(m => m.Field1) %> 
     </p> 
     <p> 
      <%= Html.TextBoxFor(m => m.Field2) %> 
     </p> 
     <p> 
      <input type="submit" value="Submit" /> 
     </p> 
    </fieldset> 
<% } %> 

那麼你的控制器動作可以執行重定向:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult HandleForm(MyModel myModel) 
{ 
    // Do whatever you need to here. 

    return RedirectToAction("OtherAction", myModel); 
} 

public ActionResult OtherAction(MyModel myModel) 
{ 
    return View(myModel);  
} 

編輯::上面的示例將現在綁定以下模型,並可以在動作之間傳遞:

public class MyModel 
{ 
    public string Field1 { get; set; } 
    public string Field1 { get; set; } 
} 
+0

如果提交包括許多領域。有沒有輸入對象? – 2012-04-02 14:47:11

+0

我修改了我的答案,以顯示如何將輸入值傳遞迴控制器操作。 – CAbbott 2012-04-02 15:02:32

1

下面的代碼演示瞭如何在用戶提交表單後將用戶重定向到其他操作。

如果您想要保留任何提交的數據以用於您要重定向的操作方法,則需要將其存儲在TempData對象中。

public class HomeController : Controller 
{ 
    [HttpGet] 
    public ActionResult Index() 
    { 
     // Get the e-mail address previously submitted by the user if it 
     // exists, or use an empty string if it doesn't 
     return View(TempData["email"] ?? string.Empty); 
    } 

    [HttpPost] 
    public ActionResult Index(string email) 
    { 
     // Store the e-mail address submitted by the form in TempData 
     TempData["email"] = email; 

     return RedirectToAction("Index"); 
    } 
} 

Index視圖會是這個樣子:

@using (Html.BeginForm("Index", "Home")) 
{ 
    @* Will populate the textbox with the previously submitted value, if any *@ 
    <input id="email" type="email" name="email" value="@Model" /> 

    <button type="submit">Submit</button> 
}