2014-03-13 45 views
2

我有一個這樣的方法在我的MVC項目:Html.BeginForm使用路由ID參數並獲取提交參數?

[Route("Categories/{categoryId}")] 
public ActionResult List(SearchQueryCommand searchQuery) { 
    //Stuff 
    return View(); 

} 

所以這種方法被稱爲像:'www.myweb.com/Categories/212'

但我有排序此查詢表單中的下拉列表,然後重新發送回服務器獲取排序結果。 我有這在我的Razor視圖:

@using (Html.BeginForm(new { categoryId = Model.CategoryId })) 
{ 
       <div> 
        <small>@Html.LabelFor(m => m.Order, "sort by")</small> 
        @Html.DropDownListFor(m => m.Order, Model.ProductSortByOptions, new { onchange = "this.form.submit();" }) 
        <button class="nojs" type="submit">ok</button> 
       </div> 
} 

我的問題是,我不知道如何格式化我的代碼在我的網址是這樣的:'www.myweb.com/Categories/213?Order=MinPrice'

我有什麼選擇來執行此操作?

回答

0

我已經找到了解決方案!並且不與Html.BeginForm

我的代碼是這樣的:

<form action="@Url.Action()" method="get"> 
    <div> 
     <small>@Html.LabelFor(m => m.Order, "ordenar por")</small> 
     @Html.DropDownListFor(m => m.Order, Model.ProductSortByOptions, new { onchange = "this.form.submit();" }) 
     <button class="nojs" type="submit">ok</button> 
    </div> 
</form> 

這總是爲我設置表單動作爲:{Categories}/{categoryId}因爲是GET,該Order將被設置爲一個查詢參數像我想要的。

+0

只是爲了澄清,這*可以用'Html.BeginForm'來完成,你只需要在表單方法之前填入所有參數,即:'Html.BeginForm(「List」,「YourController」,new {categoryId = Model.CategoryId },FormMethod.Get)'。當然,我通常採取與你在這些場景中做的相同的路徑,因爲它不那麼艱難,儘管你不需要Url.Action()位:'action =「」'也可以。 –

+0

它不會以這種方式工作......因爲它會生成一個URL,如:'www.myweb.com/YourController/List?categoryId = 123'',該URL不會與我的路由配置一起工作:'' [Route(「List/{categoryId」)]' –

+0

如果您使用的是屬性路由,您應該在'RouteConfig.cs'中註釋掉或刪除默認路由。然後,它會工作。 –

1

窗體的方法必須是一個GET

@using (Html.BeginForm(new { categoryId = Model.CategoryId }, FormMethod.Get)) 

這樣,表單提交時,所有的表單字段將被添加到URL中的查詢字符串。

要發佈到特定的操作方法,您需要在BeginForm定義中指定它們。我不是太熟悉的基於屬性的路線定義,但其中一個應該工作:

@using (Html.BeginForm("List", "Categories", new { categoryId = Model.CategoryId }, FormMethod.Get)) 

或者

@using (Html.BeginForm("Index", "Categories", new { categoryId = Model.CategoryId }, FormMethod.Get)) 
+1

是的,但這段代碼將如下的網址:'www.mypage.com/Category/Categories?categoryId=231&Order=MinPrice',這不是我正在尋找 –

1

我使用RedirectToRoute在beginform POST方法中路由params,這會生成302 http響應,但我不知道其他方法。

在routeconfig

routes.MapRoute(
      name: "list", 
      url: "{bti}-e-{bop}-e-{blo}-e-{bzo}-e-{bpr}-e-{bha}-e-{bor}", 
      defaults: new { controller = "home", action = "list", bpr = UrlParameter.Optional, bha = UrlParameter.Optional, bor = UrlParameter.Optional } 
      ); 

行動得到:

public ActionResult li(int activepage = 0, Buscador bu = null) 
    { 
    your code... 
    return View(model); 
    } 

動作後:

[HttpPost] 
    public ActionResult li(int activepage = 0,Buscador bu = null) 
    { 
    ..my code to format values... 
    ..send values below that interest me routing.. 
    return RedirectToRoute("list", new System.Web.Routing.RouteValueDictionary { { "bti", bu.bti }, { "bop", bu.bop }, { "blo", bu.blo }, { "bzo", bu.bzo }, { "bpr", bu.bpr }, { "bha", bu.bha }, { "bor", bu.bor } }); 
    } 

這項工作確定爲我。

我的英語非常糟糕。對不起

0

您需要添加方法獲取到表單標籤

@using (Html.BeginForm("MethodName", "YourController",FormMethod.Get)) 

{ ... }

然後您的Form標籤將生成GET請求,並且表單中的所有參數都將位於URL查詢字符串中。