Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajax beginForm passing route value and button value

Tags:

c#

asp.net-mvc

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

  @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
            }
like image 682
formatc Avatar asked Mar 29 '12 17:03

formatc


2 Answers

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

like image 74
AliRıza Adıyahşi Avatar answered Sep 19 '22 21:09

AliRıza Adıyahşi


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 MUST map the relative route in the RouteConfig.sc before the `default' mapRoute :

routes.MapRoute(
            name: "routeName",
            url: "myController/Index/{param1}/{param2}",
            defaults: new { controller = "myController", action = "Index", param1 = UrlParameter.Optional, param2 = UrlParameter.Optional }
        );
like image 42
Amin Saqi Avatar answered Sep 20 '22 21:09

Amin Saqi