Reputation: 46322
On submit of the page on the view, I need to call a method in the Controller. I am not sure how to do this. If you can provide a small example on how to call the Controller that would be appreciated.
Upvotes: 1
Views: 1919
Reputation: 1252
Edit: Not sure what framework you are using to develop your application, but here is an example using ASP.NET MVC.
It sounds like you want to know how to use an Action method defined in the Controller to handle the processing of a form submitted from a View. This example uses the Razor view engine. In your view, you can create the form using the BeginForm
helper method like so:
@using (Html.BeginForm("Action", "Controller", FormMethod.Post)) {
@* form elements go here *@
}
where the strings "Action" and "Controller" are the names of your action method and controller, respectively.
Note the HTML that is rendered contains the /controller/action route in the action attribute of the form tag:
<form action="/Controller/Action" method="post">
</form>
Also note that you don't have to use helper methods, you can just create your own form using the HTML above.
Upvotes: 1
Reputation: 96594
For example - Ruby on Rails:
Say you have a table/model 'buildings'
You'll have a Building
model (note - singular).
You'll have a BuildingsController
class (in a file defining that controller).
This controller will have variety of methods which (among many other uses) can be called as actions from a form, i.e. view which is your Building View.
The form code in rails is form_for :building
which build the appropriate html form tag with an action that will point to the controller method which also uses information from the model.
When the form is submitted it knows which controller method to go to based on the action and. After processing, e.g. saving to the database the controller will also then be responsible for determining which view to display (e.g. error page, list of buildings page, etc.)
As you can see in rails the standard usage of building, Building and buildings is pretty handy once you know the conventions.
Upvotes: 0