2016-02-13 54 views
0

我有以下看法如何在獲取請求時在地址欄上顯示任何值?

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

@using(Html.BeginForm("Create", "Concepts", new { name="sfsfsfsfsf", gio="sfsf9s9f0sffsdffs", ford="mtp"}, FormMethod.Get, null)) 
{ 
    <input type="submit" name="name" value="New" /> 
} 

當我點擊新建按鈕如何顯示的值giofordname的網址是什麼?

這是我的路線定義

public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

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

您添加路由值,而不是查詢字符串值。您需要定義特定的路線,或添加隱藏的輸入。顯示你的路由定義和'Create'方法的簽名(你也需要從按鈕中刪除'name =「name」') –

+0

@StephenMuecke你讓我在那裏:)什麼是路由定義及其用途 – zeelong

+0

添加代碼根據我的第一條評論,我將添加一個答案,解釋你必須做什麼(你現有的路由定義在'RouteConfig.cs'文件中定義) –

回答

1

BeginForm()您使用的是添加3個路由值,無法查詢字符串值。如果你想生成一個URL是.../Concepts/Create/sfsfsfsfsf/sfsf9s9f0sffsdffs/mtp它會去(在ConceptsController

public ActionResult Create(string name, string gio, string ford) 

然後您需要添加以下路由定義(和它需要的默認路由

routes.MapRoute(
    name: "Create", 
    url: "Concepts/Create/{name}/{gio}/{ford}", 
    defaults: new { controller = "Concepts", action = "Create" } 
); 

還請注意,您需要從提交按鈕中刪除name="name",因爲與路由參數衝突

或者,如果您想要.../Concepts/[email protected]&gio=sfsf9s9f0sffsdffs&ford=mtp,則請將路由參數d爲值添加輸入

@using(Html.BeginForm("Create", "Concepts", FormMethod.Get)) 
{ 
    <input name="name" value="sfsfsfsfsf" /> 
    <input name="gio" value="sfsf9s9f0sffsdffs" /> 
    <input name="ford" value="mtp" /> 
    <input type="submit" value="New" /> 
} 
相關問題