9

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.

Community
  • 1
  • 1
gilesrpa
  • 969
  • 1
  • 12
  • 35
  • Why you don't create 2 @using (Html.BeginForm for 2 button submit? – Linh Tuan May 13 '16 at 15:15
  • 2
    Make both your buttons have the same name, and give them different values, then check for the value submitted. – Craig H May 13 '16 at 15:21
  • 4
    I think the reason your top button is not sending the value information back is because the type is not `type="submit"`. – Craig H May 13 '16 at 15:28
  • I have changed both the the buttons too: and This is still returning null when I click either of them. – gilesrpa May 16 '16 at 07:04
  • I have looked at the proposed solution above and coded the help function as is, applied the MultipleButton attribute to the two buttons. When the extension is called, the correct values are passed in, but when the line of code controllerContext.Controller.ValueProvider.GetValue(keyValue) is executed, it always returns Null. – gilesrpa May 16 '16 at 09:12
  • From what I can see, the main problem with your above view is the name="mySubmit:RoleEdit" issue. It should read name="action:RoleEdit" – Sascha Nov 13 '19 at 17:27

1 Answers1

4

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
        }
     }
Kinjal Gohil
  • 958
  • 9
  • 15