MVC4 View Multiple Submit Buttons - Update label text

廉价感情. 提交于 2019-12-11 12:47:14

问题


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
        {

        }
    }

回答1:


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.
}



回答2:


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();
             }
          }


来源:https://stackoverflow.com/questions/19769483/mvc4-view-multiple-submit-buttons-update-label-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!