0

我需要添加一個動態routevalue在html.beginform,像這樣Html.BeginForm動態routevalues

Html.BeginForm("action", "controller", new { 

    somethingID = "some js function or jquery that get the value from a textbox" 

})) 

我知道MVC是已經經過paramenter我的方法當表單提交,但我需要包含參數的網址爲mvc url格式,即mydomain.com/controller/action/somethingID。 Eveything現在正在工作,但因爲我有另一個$.ajax()調用通過URL,如"../someMethod"$.ajax()不工作,因爲當前的網址是(mydomain.com/controller/action)而不是(mydomain.com/controller/action/somethingID)

回答

0

確保將id在RouteConfig設置爲UrlParameter.Optional

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