C# ajax beginForm 传递路由值和按钮值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9930768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:15:49  来源:igfitidea点击:

ajax beginForm passing route value and button value

c#asp.net-mvc

提问by formatc

How would I get both param1 and param2 to my controller, I tried like this but only param1 gets passed.

我如何将 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
            }

采纳答案by AliR?za Ad?yah?i

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

Is not it working ??

它不工作吗?

回答by Denny Philip

try this

尝试这个

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

回答by Amin Saqi

To set params like this:

像这样设置参数:

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

you MUSTmap the relative route in the RouteConfig.scbefore the `default' mapRoute :

必须RouteConfig.sc在 `default' mapRoute 之前映射相对路由:

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