2012-01-16 47 views
2

我在我的視圖和相應的提交按鈕上有一個表單。該表格負責選擇某些優惠搜索的選項。主頁網址如下:asp.net mvc提交表單並顯示url中的所有表單參數

http://localhost/ 

當我點擊提交按鈕時,調用適當的控制器的動作。但是,我希望所有表單的參數都暴露在url中(因此可能會共享2個人之間的鏈接,例如他們會得到相同的結果)。 因此,例如,我怎麼能實現(例如)類似的東西:

http://localhost/?startDate=20120215&endDate=20120230&catalog=ISA 

回答

3

如果您使表單的方法爲GET,所有變量將成爲查詢字符串的一部分。

您可以使用此重載以改變窗體的請求類型:

FormExtensions.BeginForm Method (HtmlHelper, String, String, FormMethod)

或者,如果你使用RedirectToAction,你可以傳遞參數的對象:

Controller.RedirectToAction Method (String, Object)

+0

是的,我試過這個,但是在我的控制器的動作被調用後,我使用RedirectToAction方法重定向到不同的控制器的動作,在視圖中我的結果列表中 – 2012-01-16 23:13:41

+0

謝謝xixonia – 2012-01-16 23:29:35

0

提交使用httpGET法的形式,讓您的形式呈現的輸出會像

<form method="get">...</form> 
1

您應該指定表單使用GET請求(而不是發佈)提交,並指定您希望重定向到的操作,以便不必使用RedirectToAction。

例如:

控制器:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    public ActionResult NextAction(IndexModel model) 
    { 
     return View(); 
    } 
} 

型號:

public class IndexModel 
{ 
    public string StartDate { get; set; } 

    public string EndDate { get; set; } 

    public string Catalog { get; set; } 
} 

檢視:

@model MvcApplication22.Models.IndexModel 

@using (Html.BeginForm("NextAction", "Home", FormMethod.Get)) 
{ 
    <p>Start Date: @Html.EditorFor(m => m.StartDate)</p> 
    <p>End Date: @Html.EditorFor(m => m.EndDate)</p> 
    <p>Catalog: @Html.EditorFor(m => m.Catalog)</p> 
    <input type="submit" value="submit" /> 
} 

但請注意,在GET HTTP請求中對系統進行任何更改不是最佳實踐。如果要進行任何更改,則應在POST請求中執行這些更改。