1

At the moment I am working on a MVC4 view with multiple submit buttons. To handle the submit of the different buttons, I use this class:

http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx

I have three buttons and one label: Start Standby Resume

How can I display a certain text in that label based on which button is pressed? I wan to use Ajax.BeginForm to update the label text (so I do not have to reload the webpage).

Thank you in advance!


Update: For example when I click at the Start Button a method will be executed. This method returns true or false. How to catch this bool and display text in the label, based on the result of the method?

Update 2:

    <div>
        <fieldset>
            <legend>Admin Form</legend>
            @Html.Label("Options")

            <div id="StartBtn">
                <input id="Start" type="submit" value="Start" name="action:Start" />
            </div>
            <div id="StandbyBtn">
                <input id="Standby" type="submit" value="Standby" name="action:Standby" />
            </div>

            <div id="ResumeBtn">
                <input id="Resume" type="submit" value="Resume" name="action:Resume" />
            </div>
        </fieldset>
    </div>   

    [MultipleButton(Name = "action", Argument = "Start")]
    public ActionResult Start()
    {
        if (start())
        {

        }
        else
        {

        }
    }
Odrai
  • 2,163
  • 2
  • 31
  • 62

2 Answers2

2

From your update I would use an ajax call instead of the ajax form

$('.btnSubmit').on('click', function(){
    $.ajax({
        url: '@Url.Action('Start', 'Controller')',
        type: 'post',
        data: {
            button: this.id
        }
        dataType: 'json',
        success: function(result){
            if(result.Success){
                $('.lblText').text(result.SetText);
            }
        }
    });
});

I don't know what you want passed to your controller but if you put the same class on all of your buttons (you need to change them to type button instead of submit also) then this.id will will be the id of the clicked button and that will be sent to the controller

then on your controller have an input field matching what is in the data field

public ActionResult Start(string button){
    //do something
    //from here http://stackoverflow.com/questions/7732481/returning-json-from-controller-never-a-success
    return Json(new { Success = "true", SetText = 'SetText' });
    //Where SetText is whatever you want your label set to.
}
Matt Bodily
  • 6,403
  • 4
  • 29
  • 48
  • But isn't it possible to create a function for all submit buttons? At the moment you have .btnOne. Also I get a few syntax errors..? – Odrai Nov 04 '13 at 14:40
  • if they all have the same class then this code will fire on each of them. just replace btnOne with that class name. You didn't post your view code so I just put place holder names in my code. If you are still getting syntax errors, Please post them and ill see if i can help you with them. – Matt Bodily Nov 04 '13 at 14:43
  • }, contentType: 'application/json; charset=utf-8', (there was no , after } – Odrai Nov 04 '13 at 14:44
  • See Update 2 in OP for code. So what to return in my public ActionResult Start() and how to catch it? – Odrai Nov 04 '13 at 14:47
  • see my edited answer. I realized I was missing the url attribute from the ajax call. From your comment I removed the line you were getting an error on. I don't think it is required. Hopefully this helps – Matt Bodily Nov 04 '13 at 15:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40507/discussion-between-odrai-and-matt-bodily) – Odrai Nov 04 '13 at 15:17
0

You can check on this post. http://www.developersnote.com/2013/02/multiple-button-in-mvc-4.html

@using (Html.BeginForm("ActionTaken", "TestController"))
      {
           <button name="button" value="ActionOne" class="button" style="width: 200px;">
              test1</button>
           <button name="button" class="button" style="width: 160px;" value="ActionTwo">
             test2</button>       
       }

 [AcceptVerbs(HttpVerbs.Post)]
          public ActionResult ActionTaken(string butt)
          {
             string ButtCommand= butt;
             switch (ButtCommand)
             {
                case "ActionOne":
                  //do stuff here
                case "ActionTwo":
                 //do stuff here
                default:
                        return View();
             }
          }
shamcs
  • 629
  • 6
  • 15