2014-02-08 32 views
0

我對asp.net mvc4有點新,現在試着去做一些練習。經過一些關於路由的研究,我仍然有一個問題在表單提交後傳遞參數。.net MVC4表單提交生成?而不是/在URL - 錯誤的路由?

我的形式如下:

@using (Html.BeginForm("Index", "Resplaner", FormMethod.Get, new { name = "navigation", id = "navigation" })) 
{ 
    <select name="id" size="15" id="nav_menu"> 
     @foreach (var categorie in Model.Categories) 
     { 
      <optgroup label="@categorie.Name"> 
       @foreach (var ressource in Model.Ressources) 
       { 
        if (@categorie.Id == ressource.Type) 
        { 
         <option value="@ressource.Id">@ressource.Name</option> 
        } 
       } 
      </optgroup> 
     } 
    </select> 
} 

這是由以下Java腳本提交:

$('#nav_menu').on('click', function (event) { 
      if ($(event.target).is("option")) { 
       var form = $(event.target).parents('form'); 
       form.submit(); 
      } 
     }); 

我的實際路由可配置是這樣的:

routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 

      routes.MapRoute(
       name: "Resplaner", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Resplaner", action = "Index", id = UrlParameter.Optional } 
      ); 

所以我問題是我現在生成的URL在表單提交後看起來像這樣

http://localhost:62456/Resplaner?id=6

,但我想要的URL看起來應該是這樣的

http://localhost:62456/Resplaner/Index/6

如果我手動輸入第二個網址給我的瀏覽器,正確的結果顯示...這就是爲什麼我想有我的表單提交方式有問題。或者我的路線還在搞亂。

我已經做了關於mvc4中的窗體的一些示例教程,但他們總是用在像我這樣的不同情況。所以我會很樂意幫助你。

我感謝每一個幫助:)

問候Kurn

+0

你沒有爲你的動作和控制器值設置'null',而是將它們放到你的'Html.BeginForm()'語句 – Tommy

+0

中,你是對的,修正它。但我的問題仍然是:) – KURN

+0

我的第二個評論是,你的兩條路線是完全一樣的。默認值不會導致路由引擎相互選擇一個。刪除你的第二條路線,讓我知道結果。 – Tommy

回答

0

這裏去我的解決方案 -

讓你的HTML這樣的 -

@using (Html.BeginForm("Index", "Resplaner", new { name = "rami", id = "1" }, FormMethod.Get)) 
{ 
    // .... 
} 

然後有一個路線這樣 -

routes.MapRoute(
     name: "MyRoute", 
     url: "{controller}/{action}/{name}/{id}", 
     defaults: new { controller = "Resplaner", action = "Index" } 
    ); 

那麼你的jQuery打表單提交,您的網址將是 -

enter image description here

和URL將是 - http://localhost:5738/Resplaner/Index/rami/1?

注:如果你不希望這個問題在最後標記,然後進行POST而不是GET。

另一種解決方案是使用隱藏字段 -

@using (Html.BeginForm("Index", "Resplaner", FormMethod.Get)) 
{ 
    @Html.Hidden("id", "1"); 
    @Html.Hidden("name", "rami"); 
    <input type="submit" value="click"/> 
} 

但同樣這種方法會給你 - http://localhost:5738/Resplaner/Index?id=1&name=rami

還有一個方法是創建具有所需的URL @Html.ActionLink()並使其點擊在jQuery中。

+0

感謝您的詳細解答。我試過你的第一個解決方案這裏的問題是,id是固定的。這個解決方案的結果就像這個'http:// localhost:62456/Resplaner/Index/1?id = 11'。今天我沒有時間進行更多的測試。你最後的解決方案似乎對我來說是最好的。我將採取行動鏈接的方式,並拋出關於表格的想法。 – KURN

+0

@KURN,你有沒有適當的路線,如我所見? – ramiramilu

+0

是的我實現了所有的代碼1:1來測試它。對我來說奇怪的是,html源代碼改成了這個'

'。但即使actionlink的方式似乎是最容易的 – KURN

0

好吧,以另一種方式修復它。

爲了解決我的實際問題,我只在控制器中添加了一個新的動作,它接收帶有問號的表單參數,並簡單地重定向到所需的URL。

[HttpGet] 
public ActionResult IndexMap(String id = "1") 
{ 
    return RedirectToAction("Index", "Resplaner", new { id = id }); 
} 

由於第二次服務器調用可能不是最好的方法。但對於我的項目,我沒有看到任何性能問題,因爲它只會在Intranet中使用。

http://localhost:62456/Resplaner/IndexMap?id=2會重定向到http://localhost:62456/Resplaner/Index/2

我想感謝大家誰試圖幫助我。你給了我一些很棒的建議和想法。