Tuesday 15 July 2014

Modifying our first MVC 4 Web Application


Apart from the generated code I’m just going to add some more pieces to this MVC Web Application. I will be publishing a full fledge ePhoneBook on ASP.NET MVC 4 later. This sample code will help you to get more understanding on MVC 4.
The first thing I’m going to do is adding a PhoneBook Model class .
Adding MVC Model
I have changed the PhoneBookModel code as below,
In this class I have mentioned the validation attribute for the elements. The ValidationAttributes are comes fromSystem.ComponentModel.DataAnnotations. There are multiple validation attributes available and all are self-descriptive.
The above image shows the available validation attributes. Read More on Validation Attribute from MSDN.
Now let’s add required views (Create, Edit, Delete, Details, List), to add the views just right click on the View folder and click on Add --> Views menu.
Now I’m going to add the Listing view, I have given the View Name as “Index” and selected the 'create a strongly typed view' Model from the Drop-down so that the code will be generated accordingly to the Model. If the created PhoneBookModel is not showing in the dropdown then build the application and then check.
select typed model - view mvc
Now we can specify which scaffolding template to use so that the code will be generated as per the template , I have selected “List” template like below
When you click on ‘OK’ button the Index.cshtml is ready.
The same way I have added all files for this sample project
The details page looks like
If you look at the code from Create template all validations are in place
Now we need to add Controller actions for these scaffolding items.
Click on the Add --->Controller menu, once you have clicked on that menu then a popup will be shown like below ,
There are multiple controller templates available in this dialogue box
  • Empty MVC controller
  • MVC controller with read/write actions and views, using Entity Framework
  • MVC controller with empty read/write actions
  • Empty API controller
  • API controller with read/write actions and views, using Entity Framework
  • API controller with empty read/write actions
I choosed “MVC controller with empty read/write actions” controller template Now give the controller name as PhoneBookController. Note that MVC requires the name of all controllers to end with "Controller".
Click on “Add” button to add the controller to our application, now the controller file is created as per our template selection and added to our project, click here to see the generated file
Now I’m going to modify the newly added controller file, the first thing I have added a property that will have a default values for the ePhoneBook
After that I have modified the Created method same as below,
Create Action MVC
If you see this method the return type is ActionResult, What’’s ActionResult. An action result represents a command that the framework will perform on behalf of the action method. The ActionResult class is the base class for action results. It Encapsulates the result of an action method and is used to perform a framework-level operation on behalf of the action method.
  • ContentResult
  • EmptyResult
  • FileResult
    • FileContentResult
    • FilePathResult
    • FileStreamResult
  • HttpUnauthorizedResult
    • HttpUnauthorizedResult
    • HttpNotFoundResult
  • JavaScriptResult
  • JsonResult
  • RedirectResult
  • RedirectToRouteResult
  • ViewResultBase
  • PartialViewResult
  • ViewResult
Read more from MSDN
lets come back to our application, The same way I have updated the Edit also
The above code will return the data for selected entry by the mobile number, Onceyou click on update button in edit page the below method will get called
For the “Delete” method we don’t have any special User Interface that’s whyonce the entry is deleted we are returning the “Index”view.
Delete Action MVC
The “Details” method is written as below
The “Index” page will display the data from the below code
Layout menu mvc
I have added a menu for Phone Book in _Layout.cshtml. Once this all done let’s “Run” our modified application,
When you click on the “Create New” the below page will be shown
Edit page looks like below, all validations are working as per the validation attributesin model.

No comments:

Post a Comment