Button event in ASP.NET MVC

I created a view page in MVC, for example

<%using (Html.BeginForm()) { %> <%=LabelHelpers.Label("firstname", "FirstName:")%> <br/> <%=Html.TextBox("firstname")%> <br/><br/> <%=LabelHelpers.Label("lastname", "Lastname:")%> <br/> <%=Html.TextBox("lastname")%> <br/><br/> <input type="Button" value="Register"/> <%} %> 

Here I want to write Buttonclick Event ... How and where should I write?

+8
asp.net-mvc
source share
2 answers

Your input is of type button - they do nothing without additional code on the client side.

If you want to handle the "event" on the server in the same way as in ASP.NET, you must convert it to a submit button. Assuming your controller is called "Account" and your action is called "Register", your current code will look something like this:

 public ViewResult Register() { return View(); } 

You want to start by passing the model into a view:

 public ViewResult Register() { var registerModel = new RegisterModel(); return View(registerModel); } 

Your current view uses weakly typed inputs. Since you pass it a model, you can use strongly typed views. Your model should look something like this:

 public class RegisterMode { public string Firstname { get; set; } public string Surname { get; set; } } 

To use strongly typed views, change your view like this:

 <%using (Html.BeginForm()) { %> <%=Html.LabelFor(x => x.Firstname)%> <br/> <%=Html.TextBoxFor(x => x.Firstname)%> <br/><br/> <%=Html.LabelFor(x => x.Surname)%> <br/> <%=Html.TextBoxFor(x => x.Surname)%> <br/><br/> <input type="submit" value="Register"/> <%} %> 

We were asked to create shortcuts and text fields for your RegisterModel type. This will automatically display the model values ​​when submitting the form to the controller.

Accept the message, we need to add a new action to the controller with the same name, but accept a parameter of type RegisterModel:

 public ActionResult Register(RegisterModel model) { // do something with the model, such as inserting it into the database. // model.Firstname will contain the value of the firstname textbox // model.Surname will contain the value of the surnaem textbox return RedirectToAction("Success"); } 

The last thing you need to do to be safe is to add the [HttpGet] and [HttpPost] to your controller actions to control the methods they take:

 [HttpGet] public ViewResult Register() 

and

 [HttpPost] public ActionResult Register(RegisterModel model) 

I suggest you read on MVC http://www.asp.net/mvc and read the NerdDinner chapter in the "Professional MVC" section (available for free online viewing in PDF format).

+12
source share

joins the question, wants to make it more specific

I have a form, the form already has a submit button, but I need to associate an additional action with another button. yes, I know that MVC does not support events because HTML forms do not support them.

so the solution I came to is to create hidden inputs inside the form and associate the onclick event (jquery 'live') with each ... oh, what the hell? here is the code:

HTML:

 <input type="hidden" id="SenderControlID" name="SenderControlID" value="-1" /> <input type="hidden" id="SenderControlValue" name="SenderControlValue" value="-1" /> 

JS:

  if ($('#SenderControlID')[0]) { $('input[type="submit"], input[type="button"], input[type="checkbox"], input[type="radio"]').live('click', function () { $('#SenderControlID').val($(this).attr('name')); $('#SenderControlValue').val($(this).val()); }); } 

but maybe there is a more elegant solution?

+1
source share

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


All Articles