MVC razor form with multiple different submit buttons?

前端 未结 13 1948
-上瘾入骨i
-上瘾入骨i 2020-12-04 12:59

A Razor view has 3 buttons inside a form. All button\'s actions will need form values which are basically values coming input fields.

Every time I click any of butto

相关标签:
13条回答
  • 2020-12-04 13:29

    Try wrapping each button in it's own form in your view.

      @using (Html.BeginForm("Action1", "Controller"))
      {
        <input type="submit" value="Button 1" />
      }
    
      @using (Html.BeginForm("Action2", "Controller"))
      {
        <input type="submit" value="Button 2" />
      }
    
    0 讨论(0)
  • 2020-12-04 13:30

    As well as @Pablo's answer, for newer versions you can also use the asp-page-handler tag helper.

    In the page:

    <button asp-page-handler="Action1" type="submit">Action 1</button>
    <button asp-page-handler="Action2" type="submit">Action 2</button>
    

    then in the controller:

        public async Task OnPostAction1Async() {...}
        public async Task OnPostAction2Async() {...}
    
    0 讨论(0)
  • 2020-12-04 13:32

    This elegant solution works for number of submit buttons:

    @Html.Begin()
    {
      // Html code here
      <input type="submit" name="command" value="submit1" />
      <input type="submit" name="command" value="submit2" />
    
    }
    

    And in your controllers' action method accept it as a parameter.

    public ActionResult Create(Employee model, string command)
    {
        if(command.Equals("submit1"))
        {
          // Call action here...
        }
        else
        {
          // Call another action here...
        }
    }
    
    0 讨论(0)
  • 2020-12-04 13:33

    In case you're using pure razor, i.e. no MVC controller:

    <button name="SubmitForm" value="Hello">Hello</button>
    <button name="SubmitForm" value="World">World</button>
    @if (IsPost)
    {
        <p>@Request.Form["SubmitForm"]</p>
    }
    

    Clicking each of the buttons should render out Hello and World.

    0 讨论(0)
  • 2020-12-04 13:36

    This is what worked for me.

    formaction="@Url.Action("Edit")"
    

    Snippet :

     <input type="submit" formaction="@Url.Action("Edit")" formmethod="post" value="Save" class="btn btn-primary" />
    
    <input type="submit" formaction="@Url.Action("PartialEdit")" formmethod="post" value="Select Type" class="btn btn-primary" />
    
     [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit( Quote quote)
            {
               //code 
           }
     [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult PartialEdit(Quote quote)
            {
               //code
            }
    

    Might help some one who wants to have 2 different action methods instead of one method using selectors or using client scripts .

    0 讨论(0)
  • 2020-12-04 13:37

    You could also try this:

    <input type="submit" name="submitbutton1" value="submit1" />
    <input type="submit" name="submitbutton2" value="submit2" />
    

    Then in your default function you call the functions you want:

    if( Request.Form["submitbutton1"] != null)
    {
        // Code for function 1
    }
    else if(Request.Form["submitButton2"] != null )
    {
        // code for function 2
    }
    
    0 讨论(0)
提交回复
热议问题