开发者

Button Event in ASP.NET MVC

I have created view page in MVC like

<%using (Html.BeginForm())
{ %>
    <%=LabelHelpers.Label("firstname", "FirstName:")%>
    <br/>
    &l开发者_如何学JAVAt;%=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?


Your input is of type button - these don't do anything without additional client side code.

If you want to handle the 'event' on the server in a similar way that you would have in ASP.NET, you should convert it to a submit button. Assuming your controller is called 'Account' and your action is called 'Register' your current code would look something like this:

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

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

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

    return View(registerModel);
}

Your current view is using loosely typed inputs. Since you're passing 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 to look 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"/>
<%} %>

What we've done is told the view to build labels and text boxes for your RegisterModel type. This will allow the model values to be automatically mapped when you POST the form to the controller.

Do accept the post, we need to add a new Action to the controller, with the same name, but accepting 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");
}

One last thing to do, to be safe, is to add the [HttpGet] and [HttpPost] attributes to your controller actions to control the methods they accept:

[HttpGet]
public ViewResult Register()

and

[HttpPost]
public ActionResult Register(RegisterModel model)

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


joining to the question, want to make it more concrete

i have a form, the form already has a submit button, but i need to bind an additional action to another button. yes, i do know that MVC does not support events 'cause HTML forms doesn't support them.

so the solution i've came to is to create to hidden inputs inside the form and bind an 'onclick' event (jquery 'live' method) to every... 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 more elegant solution?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜