Using 2 buttons in one ASP.NET MVC form

In general, is it possible to have two different buttons in the same form that are sent to different controller actions in ASP.NET MVC?

I try to have two input tags (type = "button") in the same form, but I want them to perform different actions with the controller. I would like to do this on several occasions, because I believe that it provides good aesthetics to be able to click buttons, not hyperlinks. Is there a way to do this or should I design it differently?

+3
source share
9 answers

Some thoughts on using this in a browser:

  • , . , .
  • , ; javascript, .
+1

Javascript. Javascript , .

 $(function() {
      $('#button1').click( function() {
          $(form).attr( 'action', '<% Url.Action( "action1" ) %>' )
                 .submit();
          return false; // prevent default submission
      });
      $('#button2').click( function() {
           $(form).attr( 'action', '<% Url.Action( "action2" ) %>' )
                 .submit();
          return false; // prevent default submission
      });
}); 
+9

, , - "" "", .

"", "" "". . , "AcceptFormValueAttribute". , Delete [Get] , .

    [ActionName("Edit")]
    [AcceptFormValue(Name = "Action", Value = "Delete")]
    [AcceptVerbs(HttpVerbs.Post)]
    [ValidateAntiForgeryToken]
    public ActionResult EditDelete(int? id)
    {
        return RedirectToAction("Delete", new { id = id });
    }
+1

ASP.NET MVC, html. , , , - javascript , .

0

, . , , 3. , 1 - (, TempData).

  • , , RedirectToAction(). ( )
  • ()
  • input type="button" onclick ()
0

, , , VIA http POST, , - :

<input type="submit" name="GO" ID="GO" value="GO BUTTON" />
<input type="submit" name="STOP" ID="STOP" value="STOP BUTTON" />

mvc : go, .

0

# 1

, , CSS , () "" ?

:

<fieldset id="CombinedForm">

<form ... action="Method1">
  ...form stuff here...
  <input id="Button1" type="submit" value="Do something">
</form>

<form ... action="Method2">
  ...form stuff here...
  <input id="Button2" type="submit" value="Do something else">
</form>

</fieldset>

... CSS :

#CombinedForm {
  position: relative;
  padding-bottom: 2em; /* leave space for buttons */
}

#Button1, #Button2 {
  position: absolute;
  bottom: 0;
}

#Button1 {
  left: 0;
}

#Button2 {
  right: 0;
}

, fieldset div, , HTML-, , , .

# 2

: , , ( ), .

0

Javascript...

public ActionResult DoSomething()
{
     // -
    TempData [ "FormValues" ] = Request.Form;
    if (Request.Form( "ButtonA" )!= null)
      {
        return RedirectToAction ( "ActionA", RouteData.Values);
     }
      return RedirectToAction ( "ActionB", RouteData.Values);
}

0

, .

, : ButtonA, ButtonB . : BeginForm:

@using ( Html.BeginForm("ButtonA" , "Home") )
{
        <input type="submit" value="ButtonA"/>
}

@using ( Html.BeginForm("ButtonB" , "Home") )
{
        <input type="submit" value="ButtonB" />
}

You must also declare the actions of ButtonA, ButtonB on the Home Controller:

[HttpPost]
public ActionResult ButtonA()
{ . . . }

[HttpPost]
public ActionResult ButtonB()
{ . . . }
0
source

Source: https://habr.com/ru/post/1716120/


All Articles