Search code examples
model-view-controllerrazorenginesubmit-button

Cannot detect which Submit Button is pressed


I have two input type submit on my view.

  1. <input type="submit" name="submitbutton1" value="Save">

2.<input type="submit" name="submitbutton2" value="Process">

My view is Manage.vbhtml and in that view i have a form with the above two submit buttons.

My controller is StaffController and 'Function Manage(item as staff,submitbutton1 as string,submitbutton2 as string) as ActionResult' is associated with the above view. When i click any of the two submit buttons i should be getting the value of that button in the string parameter, but it's giving me nothing. Please help me, i want to detect which submit button was pressed. The mentioned actionresult has <HttpPost> _ in the attribute.

I have followed as per this link but still no result.


Solution

  • You can check it from FormCollection:

    in Html:

    <input type="submit" name="submitbutton" value="Save">
    <input type="submit" name="submitbutton" value="Process">
    

    and in Action:

    [HttpPost]
    public ActionResult Manage()
    {
    
       var temp = Request.Form["submitbutton"].ToString();
    
       if(temp == "Save")
       {
         // save clicked
       }
       if(temp == "Process")
       {
         // Process clicked
       }
    
        return View();
    
    }
    

    or:

    [HttpPost]
    public ActionResult Manage(FormCollection form)
    {
    
       var temp = form["submitbutton"].ToString();
    
       if(temp == "Save")
       {
         // save clicked
       }
       if(temp == "Process")
       {
         // Process clicked
       }
    
        return View();
    
    }