ajax beginForm传递路由值和按钮值

时间:2012-03-29 17:56:12

标签: c# asp.net-mvc

如何将param1和param2同时送到我的控制器,我试过这样但只传递了param1。

  @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
            }

3 个答案:

答案 0 :(得分:12)

@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
        }

不工作吗?

答案 1 :(得分:3)

设置这样的参数:

@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
    }

必须在'默认'mapRoute之前映射RouteConfig.sc中的相对路线:

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

答案 2 :(得分: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
}