2

I am working on a MVC web application and not sure where I should add my code to handle button clicks. I have a cshtml view that is being rendered by a controller. When the view is displayed I need to handle several buttons that are being displayed - such print, email, etc. Where should I add my code to handle these and can you give me an example on how to implement it?

<input id="save-button" type="submit" class="btn btn-primary" onclick="submitOrder();" value="Print Order" />

The submitOrder() call is what I need to handle.

Chad McGrath
  • 1,561
  • 1
  • 11
  • 17
Craig
  • 1,205
  • 2
  • 21
  • 54
  • Other than the submit button html, can you show us some code that you have already tried? Also can you specify if you want to sumbit form data to multiple controller actions or do you only have one submit function? Also do you even hit the `submitOrder()` when you click on the `save-button`? If you have a `` type of `submit` you do not need to have a `onclick` event on your button. – jacqijvv Mar 09 '14 at 17:09
  • possible duplicate of [How do you handle multiple submit buttons in ASP.NET MVC Framework?](http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework) – Brad Rem Mar 09 '14 at 17:57
  • I don't have any code to show as I am not sure where to add the code. I would like to the submit button to call code in the controller, but is that the right way it should work in a MVC application. – Craig Mar 09 '14 at 17:57
  • it looks like from your question you are thinking of code behind , which is not possible in asp.net mvc.. submit is form post , onclick is javascript actions. – Vishal Sharma Mar 09 '14 at 17:58

1 Answers1

3

1.If your button is not posting back to a form you can call the controller like this:

    <input type="button" value="Something" onclick="location.href='@Url.Action("ActionName", "ControllerName")'" />

there are several overloads to this method, some accepting many more parameters.

2.If you are posting back a form and your input button is within that form, then your input button will post back to the controller.

The login page in the default mvc application has examples of both of the 2 options I've mentioned above.

Here's a part of that page:

     <section id="loginForm">
        @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class =     "form-horizontal", role = "form" }))
        {
            @Html.AntiForgeryToken()
            <h4>Use a local account to log in.</h4>
            <hr />
            @Html.ValidationSummary(true)
            <div class="form-group">
                @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
                <div class="col-md-10">
                    @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.UserName)
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
                <div class="col-md-10">
                    @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Password)
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <div class="checkbox">
                        @Html.CheckBoxFor(m => m.RememberMe)
                        @Html.LabelFor(m => m.RememberMe)
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Log in" class="btn btn-default" />
                </div>
            </div>
            <p>
                @Html.ActionLink("Register", "Register") if you don't have a local account.
            </p>
        }
     </section>

You also have several options to call a controller using JQuery Ajax. That's a little more sophisticated, though.

Chad McGrath
  • 1,561
  • 1
  • 11
  • 17