Reputation: 1253
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 :)
Upvotes: 1
Views: 4642
Reputation: 2880
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)) {
Upvotes: 0
Reputation: 101681
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.
Upvotes: 1
Reputation: 15138
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 ...
}
Upvotes: 1