Reputation: 2982
I am new into the MVC environment and I am working through the MVC3 sample app that is created in VS 2010 (File -> New -> Project -> MVC3 Web Application).
This application has a "Login" link towards the top right of the web page. Clicking the login page displays a page with your basic userID and password text fields and a submit button.
In the LogOn.cshtml page, the submit button html code is:
<p>
<input type="submit" value="Log On" />
</p>
On the AccountController.cs page, public ActionResult LogOn(LogOnModel model, string returnUrl)
function is called.
Question: How does the Submit button know which function on AccountController.cs to call?
My confusion is caused by the fact that in aspx pages, there is likely a Button.OnClick Method which tells the button what function to execute.
Upvotes: 2
Views: 5027
Reputation: 1571
I suggest you to handle ajax post using jquery to handle button click event. Try the below method.
<input type="button" value="Button 1" class='button1' />
[Script]
<script type="text/javascript">
$('.button1').click(function () {
$.ajax({
type: "POST",
url: '@Url.Action("ActionName", "ControllerName")',
dataType: "html",
success: function (result) {
// after the post action
}
});
});
Upvotes: -1
Reputation: 1991
hi i will give you an example like below
In you controller you have 2 action method like (In Example i have country controller)
(1)
[HttpGet]
public ActionResult Create()
{
return View();
}
(2)
[HttpPost]
public ActionResult Create(Country country)
{
if (ModelState.IsValid)
{
CountryRepository.InsertOrUpdate(country);
CountryRepository.Save();
return RedirectToAction("Index");
}
return View(country);
}
In asp.net web form your are using Page Load Function. same way in mvc [HttpGet] Create Action you are using like page load function.
In your View
@using (Html.BeginForm("Create", "Country", FormMethod.Post))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Country</legend>
@Html.Partial("_CoutnryAddOrEdit", Model)
<p>
<input type="submit" value="Create" id="btnSave" />
</p>
<br />
<p>
<input type="submit" value="GetData" id="btnGetData" />
</p>
</fieldset>
}
In Html.BeginForm function have different parameter.In that function you pass parameter value like first parameter is your "action Name", second is "Controller Name", third is "Your FormMethod like GET or POST"
1) Get=> Get method called when your page Lode
2) POST => Post method called after you click on your button.
so your Html.BeginForm look like this Html.BeginForm("Create","Country",FormMethod.Post).
so after click in button which you wont any action then you all code write on Post Method .
i think this will help you.
Upvotes: 0
Reputation: 1
On the click of submit button in mvc, form will be submitted to the action which are defined inside our form action attribute. if action is not defined in the form it will submit the form to the same action name which it came from and which filter is [Httppost]
Upvotes: 0
Reputation: 7443
Through the Routing engine. The application will have a route similar to
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Essentially the action used by the form in this case is /Account/LogOn. Looking at the main route this translates to controller = "Account" and Action = "Logon".
This is a birds eye view of the mechanism. The ASPNET MVC site has a great series of tutorials to describe the basics of MVC.
EDIT
In HTML a submit button is used to send form data to a server. The data is sent to the page specified in the form's action attribute. The file defined in the action attribute usually does something with the received input:
The html code
<input type="submit" value="Log On" />
will be wrapped in a form tag in the cshtml page
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
//html here
}
When rendered in the browser the form tag looks like
<form action="/Account/Logon" class="form-horizontal well" method="post" novalidate="novalidate">
<input id="ReturnUrl" name="ReturnUrl" type="hidden" value="/">
<!-- other HTML elements here -->
<input type="submit" value="Log On" />
</form>
Therefore when the submit button is clicked the form is posted (HTTP Verb POST) to the url in the action attribute of the form tag. In this case /Account/Logon.
Further information can be read in the W3C Form Recommendation
The biggest step from WebForms to MVC is that WebForms abstracts so much of the underlying HTML Specifications away from the developer.
Upvotes: 2