1

Ive just started to play around with ASP MVC and I come from a background of Web Forms. Ive started a MVC internet application and wanted to know how a button calls an action from a controller. Here I would like to use the log in example that is provided with an MVC Internet application.

AccountController:

Lots of methods

Login View:

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Log in Form</legend>
    <ol>
        <li>
            @Html.LabelFor(m => m.UserName)
            @Html.TextBoxFor(m => m.UserName)
            @Html.ValidationMessageFor(m => m.UserName)
        </li>
        <li>
            @Html.LabelFor(m => m.Password)
            @Html.PasswordFor(m => m.Password)
            @Html.ValidationMessageFor(m => m.Password)
        </li>
        <li>
            @Html.CheckBoxFor(m => m.RememberMe)
            @Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
        </li>
    </ol>
    <input type="submit" value="Log in" />
</fieldset>

so when I hit the sumbit button on the view, which method in the AccountController class will be called? and how do you work it out?

Thanks all :)

3 Answers 3

1

When you click submit button you send a post request,and in your AccounController this Controller Action should called:

[HttpPost]
public ActionResult Login(Usermodel model)
{

}

I suggest you to watch this tutorial to learn MVC Controllers,Actions and some other stuff.It is a good training.

Edit: In MVC when you type a URL like localhost/Home/Index first it goes Home Controller and looking for an Index action, and it must be [HttpGet] action because your request is a Get request. But you don't need to mark your Action with HttpGet Attribute because it's default behaviour.It works like this way because of your RouteConfig.Your controllers return Views, if you look at your HomeController and AccountController you will see all actions returning a View, and if you Right click your action and click Go To the View you will see your View which belongs to your controller.

Sign up to request clarification or add additional context in comments.

3 Comments

How do you know the model will be call Login? as i cant see any reference to it on the view. Also how is a model being created here?
i added some explanation but it will be better if you watch the video which i suggest you,my english isn't good enough =))
Thanks for your help and i shall watch those videos that you sent me :)
1

I presume you're talking about the default ASP.NET MVC Template, in this case you'll see that in the Login.cshtml view, you have a model defined at the top:

@model MyApp.Models.LoginModel

This will bind your view to this model. When you do a POST it will gather html elements from your form with names corresponding to the model properties.

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
   ...
}

This here creates a html form that will do a post to /Account/Login. However you can specify a different location:

@using (Html.BeginForm("Index", "Home", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post)) {
   ...
}

On the server side you'll be expecting the same model:

public ActionResult Login(LoginModel model, string returnUrl)
{
    // awesomeness ...
}

Comments

0

Ive just started to play around with ASP MVC and I come from a background of Web Forms

use ajax post on button click as same in aspx with url as action name and controller name, this is the closest you can get with webforms

$.ajax({
            url: '@Url.Action("SendMail", "Contact")',
            type: 'POST',
            data: { listID: selected.toString(), flag: flag },
            traditional: true,
            success: function (result) {

            }
        });

or you can use whole form post using default way in MVC

[HttpPost]
public ActionResult SendMail(Mail model)
{

}

coz you have defined

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {

on your view, which will point to default action through route, However you can change the default action explicitly defining desired action name and controller name using

@using (Html.BeginForm("ActionName", "ControllerName", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post)) {

4 Comments

why is this negative??
as I said you can on your button click you can have an ajax post with parameters if you need, with action and controller name on url
he don't want use ajax, he wants to understand MVC logic
@Selman22 this was the nearest possible logical explanation to him being a web form dev and before he does some research

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.