2012-03-29 27 views
4

如何將param1和param2都傳遞給我的控制器,我這樣試過,但只有param1被傳遞。ajax beginForm傳遞路由值和按鈕值

@using (Ajax.BeginForm("Index", "Controller", new { param1 = 0 }, new AjaxOptions { UpdateTargetId = "Target", InsertionMode = InsertionMode.Replace, OnFailure = "error" })) 
       { 
        <input type="submit" name="param2" value="1" /> 
    //more buttons 
       } 


    public ActionResult Index(int param1, int param2) 
      { 
       //do something 
      } 

回答

11
@using (Ajax.BeginForm("Index", "Controller", new { param1 = 0 }, new AjaxOptions { UpdateTargetId = "Target", InsertionMode = InsertionMode.Replace, OnFailure = "error" })) 
      { 
       <input type="submit" name="param2" id="param2" value="1" /> 
//more buttons 
      } 


public ActionResult Index(String param1, String param2) 
     { 
      //do something 
     } 

是不是它的工作?

+2

啊..我想避免使用隱藏的領域,無論如何感謝這將做! – formatc 2012-03-29 18:40:34

+2

這段代碼在我的項目中工作。 – 2012-03-29 18:49:40

+0

是的,它的工作原理,但我希望解決方案,而無需添加額外的HTML頁面。 – formatc 2012-03-29 19:24:48

0

試試這個

@using (Ajax.BeginForm("Index", "Controller", new { param1 = 0 **,param2=1** }, new AjaxOptions { UpdateTargetId = "Target", InsertionMode = InsertionMode.Replace, OnFailure = "error" })) 
{ 
    <input type="submit" name="param2" value="1" /> 
    //more buttons 
} 

public ActionResult Index(int param1, int param2) 
{ 
    //do something 
} 
+1

param2的是取決於按鈕動態值按下。 – formatc 2012-03-29 18:39:19

3

要設置PARAMS這樣的:

@using (Ajax.BeginForm("Index", "myController", new { param1 = 0, param2 = 1 }, new AjaxOptions { UpdateTargetId = "Target", InsertionMode = InsertionMode.Replace, OnFailure = "error" })) 
     { 
      <input type="submit" /> 
      //more buttons 
     } 


    public ActionResult Index(String param1, String param2) 
    { 
     //do something 
    } 

必須的'默認」圖路線前映射在RouteConfig.sc相對路徑:

routes.MapRoute(
      name: "routeName", 
      url: "myController/Index/{param1}/{param2}", 
      defaults: new { controller = "myController", action = "Index", param1 = UrlParameter.Optional, param2 = UrlParameter.Optional } 
     );