In the previous article, Creating a basic ASP.NET MVC Application-Part I we discussed about creating a User Interface to a Model defined as Student. Then we build and run the application. Going back to the application, The Controller named StudentController calls the Index method so, the Index.cshtml of the Student sub-folder in the Views folder will be deployed. In the Index.cshtml view, we do have referred the Student Model in the Models folder.
Now we are going to perform a Database insert operation. First create a Database that matches the data types described in the Student class of the Models folder. The Database insertion operation must happen in the StudentController.
Currently, we do have a method defined named Index. By default, this method is called. We can define another method to be invoked when a HttpPOST call is being made. In detail, it is the method that will be executing when I click theCreate button of that form.
In general, there is a rule that a method in a Controller cannot be overloaded. Here I create the method that will invoke in an HttpPOST call.
As you can see here, A Student object named student will be passed into the method. This is method will be invoked when a HttpPost call is made. Likewise, there are calls that can be defined for HttpGet, HttpDelete and HttpPut as well.
Set the default landing URL
Once you debug and run the application, it will show an error message. Eg: http://localhost:49366/ this URL is not working for you.(port name may change) You will have to browse it as http://localhost:49366/Student/. This is because of the default URL settings of the application. By default, your application will map the Index method of the HomeController, which we have not defined in this application. If you create an internet or intranet application, HomeController comes automatically created.
It is defined in the Global.asax.cs file of the Project.
You can replace the Home (controller name) to Student and run the application. Then the Index method of the StudentController. If you do need to change the invoking method, then change the assigned value of Index to your method. Here the id is an optional parameter. If you do need to get use of it, your invoking method must have an int type parameter named id. Likewise, you can do many customization over here.
Lets discuss in detail about the Model in next article. Till then, go through the coding that you have done and generated up to now.
Post a Comment