In this section we will create a simple customer model, flourish the same with
some data and display the same in a view.
class with 3 properties code, name and amount. Create a simple MVC project,
right click on the model folder and click on add new item as shown in the below
figure.
From the templates select a simple class and name it as customer.
Create the class with 3 properties as shown in the below the code snippet.
The next step is to add the controller and create a simple action display
customer as shown in the below code snippet. Import the model namespace in the
controller class. In the action we created the object of the customer class,
flourished with some data and passed the same to a view named as “DisplayCustomer”
view folder and click add view. You should see a drop down as shown in the below
figure. Give a view name, check create a strongly typed view and bind this view
to the customer class using the dropdown as shown in the below figure.
The advantage of creating a strong typed view is you can now get the
properties of class in the view by typing the model and “.” as shown in the
below figure.
Below is the view code which displays the customer property value. We have
also put an if condition which displays the customer as privileged customer if
above 100 and normal customer if below 100.
some data and display the same in a view.
Step1:- Create a simple class file
The first step is to create a simple customer model which is nothing but aclass with 3 properties code, name and amount. Create a simple MVC project,
right click on the model folder and click on add new item as shown in the below
figure.
From the templates select a simple class and name it as customer.
Create the class with 3 properties as shown in the below the code snippet.
public class Customer { private string _Code; private string _Name; private double _Amount; public string Code { set { _Code = value; } get { return _Code; } } public string Name { get { return _Name; } set { _Name = value; } } public double Amount { set { _Amount = value; } get { return _Amount; } } }
Step2:- Define the controller with action
The next step is to add the controller and create a simple action display customer as shown in the below code snippet. Import the model namespace in the
controller class. In the action we created the object of the customer class,
flourished with some data and passed the same to a view named as “DisplayCustomer”
public class CustomerController : Controller { ….. …. public ViewResult DisplayCustomer() { Customer objCustomer = new Customer(); objCustomer.Id = 12; objCustomer.CustomerCode = "1001"; objCustomer.Amount = 90.34; return View("DisplayCustomer",objCustomer); } }
Step3:- Create strongly typed view using the class
We need to now join the points of MVC by creating views. So right click on theview folder and click add view. You should see a drop down as shown in the below
figure. Give a view name, check create a strongly typed view and bind this view
to the customer class using the dropdown as shown in the below figure.
The advantage of creating a strong typed view is you can now get the
properties of class in the view by typing the model and “.” as shown in the
below figure.
Below is the view code which displays the customer property value. We have
also put an if condition which displays the customer as privileged customer if
above 100 and normal customer if below 100.
<body> <div> The customer id is <%= Model.Id %> <br /> The customer Code is <%= Model.CustomerCode %> <br /> <% if (Model.Amount > 100) {%> This is a priveleged customer <% } else{ %> This is a normal customer <%} %> </div> </body>
Step 4 :- Run your application
Now the “D” thing, hit cntrl + f5 and you will get the output.
No comments:
Post a Comment