18 April 2014

Using a Model To Pass Information To Our View -- Basics MVC(Model View Controller)

    Basically a view template is an HTML output for viewers, if we want to create dynamic web pages we need to pass information from controller to views. Model class represents to the tables in our database and Model classes can be used to access database values, these Model data passes to views to create webpages that is done by the controller. 

Let us go through the Instance

Create Context class 'New database' and model class for representing Country table in our database, 'Country.cs' as explained earlier in Accessing Model Data From Controller  Add Action Method in CountryController.
Public ActionResult country Details (int id)
 {
 
            Country country = db.Country.Find (id);
            Return View (country);
   }
Add a View for the action method..Right Click inside 'country Details' >>Add View


Country Details.cs
@model MvcApplication2.Models.Country
@{
    ViewBag.Title = "countryDetails";
}

<h2>countryDetails</h2>
Country name: @Html.DisplayFor(model => model.country_name)


 

Run the project with Url: ../Country/countryDetails/1


No comments:

Post a Comment