Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 5 multiple submit on one view [duplicate]

I know that this has been asked multiple times. But so far I have not found a working solution.

I have the following code in a view:

@using (Html.BeginForm("RoleEdit", "AccountRoles", FormMethod.Post, new { id = "updaterole" }))
{
    <button name="mySubmit" formaction="@Url.Action("SetDefaultRole", "AccountRoles")" value="MakeDefault" class="button small">Make Default</button>

    ...


    other code

    ...

    <button type="submit" class="button small" value="save" name="save" id="save">@Resources.DashBoardTags.UpdateRoleTag</button>
}

And in the controller:

   [HttpPost]
    public ActionResult SetDefaultRole()
    {

        return View();
    }

and:

    [HttpPost]
    public ActionResult RoleEdit(List<UserRole> userRole, string mySubmit)
    {

        if (mySubmit == "Save Draft")
        {
            //save draft code here
        }
        else if (mySubmit == "Publish")
        {
            //publish code here
        }
     }

When the code executes:

  1. When clicking on the first submit button it ignores the SetDefaultRole function and executes the RoleEdit function.

  2. The RoleEdit function value mySubmit is null.

Please show my the error of my ways !!!!

I have looked at the proposed solution: Proposed Solution

From this I have created an attribute extension called MultipleButton and changed my code so that the the code now looks like this:

View:

@using (Html.BeginForm("RoleEdit", "AccountRoles", FormMethod.Post, new { id = "updaterole" }))
{
   <button value="SetDefaultRole" name="action:SetDefaultRole" class="button small" formmethod="post">Make Default</button>

    ...


    other code

    ...

    <button value="RoleEdit" name="mySubmit:RoleEdit" class="button small" formmethod="post">@Resources.DashBoardTags.UpdateRoleTag</button>
}

Controller

    [HttpPost]
    [MultipleButton(Name= "action", Argument = "SetDefaultRole")]
    public ActionResult SetDefaultRole()
    {

        return View();
    }

    [HttpPost]
    [MultipleButton(Name = "action", Argument = "RoleEdit")]
    public ActionResult RoleEdit(List<UserRole> userRole)
    {
        return View();
    }

Within the new extension, MultipleButtonAttribute shown in the Proposed Solution link above, is executed, the following line of code always returns a null value:

controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument;

Can anyone help.

like image 277
gilesrpa Avatar asked May 13 '16 15:05

gilesrpa


1 Answers

you can identify your button from there name tag like below, Your need to check like this in you controller

if (Request.Form["mySubmit"] != null)
{
//Write your code here
}
else if (Request.Form["save"] != null)
{
//Write your code here
}

OR try;

[HttpPost]
    public ActionResult RoleEdit(List<UserRole> userRole, FormCollection fc)
    {

        if (fc["mySubmit"] != null)
        {
            //save draft code here
        }
        else if (fc["mySubmit"] != null)
        {
            //publish code here
        }
     }
like image 161
Kinjal Gohil Avatar answered Oct 02 '22 12:10

Kinjal Gohil