View Data
Each Controller class has a property called ViewData . this property is used to pass data between the controller and the view.
e.g :
public ActionResult
getArrayresult()
{
ArrayList arr = new
ArrayList();
arr.Add("Value1");
arr.Add("Value2");
ViewData["message"] = arr;
return View();
}
<% ArrayList
temp =(ArrayList)ViewData["message"]; %>
<%= temp[1] %>
Output : Value 2
Each Controller class has a property called ViewData . this property is used to pass data between the controller and the view.
e.g :
public ActionResult
getArrayresult()
{
ArrayList arr = new
ArrayList();
arr.Add("Value1");
arr.Add("Value2");
ViewData["message"] = arr;
return View();
}
<% ArrayList
temp =(ArrayList)ViewData["message"]; %>
<%= temp[1] %>
Output
: Value 2
Strongly-Typed
View
The easiest way to
create a strongly-typed view is to use the "Add View" dialog below.
Examples
Classes
:
public class Fund
{
public int
NumberOfFunds { get; set;
}
public List<string> FundsList { get;
set; }
private int _FundID;
private string
_Fundname;
public int FundID
{
get { return _FundID;
}
set { _FundID = value;
}
}
public string
FundName
{
get { return
_Fundname; }
set { _Fundname = value;
}
}
}
public class Price
{
private int _PriceID;
private string
_Price;
public int PriceID
{
get { return
_PriceID; }
set { _PriceID = value;
}
}
public string
FundPrice
{
get { return _Price;
}
set { _Price = value;
}
}
}
Application :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcTextApplication.Controllers
{
public class ExperimentsController : Controller
{
//The view data class exposes only one
Model property.
//So what do you do when you need to
pass multiple data items from a controller to a view?
//For example, To pass both a
collection of Funds and price to a view using a typed view is to use a view
model.
// simple View Model container to pass
multiple business entities (and potentially other types) to the view
public class ProductViewModel
{
public ProductViewModel(List<Fund> fnd, List<Price> price)
{
this.Fund = fnd;
this.Price = price;
}
public List<Fund> Fund { get;
private set; }
public List<Price> Price { get;
private set; }
}
//
Strongly-Typed View In this case, the collection is assigned to the
ViewData.Model property
public ActionResult
Method1()
{
Fund ObjFunds = new
Fund();
ObjFunds.FundID = 10001;
ObjFunds.FundName = "Tsting Fund";
return View(ObjFunds);
//or below code BOTH are SAME
//ViewData.Model = ObjFunds;
//return View();
}
<%@ Control Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<Fund>" %>
<h3>Funds
Data:</h3>
<br>
<p><b>FundID
:</b> <%= Model.FundID %></p>
<p><b>Fund
Name:</b> <%= Model.FundName %></p>
Note :
Abouve Last bit is
important. Note that the ViewPage class is (or can be)
generically typed, allowing a specific type to be defined for that ViewPage. When you create a strongly-typed view, you are
tying that ViewPage to a specific type of model (as in
"model-view-controller").
Normally it is Namespace.Class or Classname
Output
:
FundID
: 10001
Fund
Name: Testing Fund
//Not Strongly-Typed View
public ActionResult
Method2()
{
Fund ObjFunds = new
Fund();
ObjFunds.FundID = 10001;
ObjFunds.FundName = "Testing Fund
Without Class";
ViewData["Fund"] =
ObjFunds.FundName.ToString() ;
return View();
}
<%@ Control Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%: ViewData["Fund"]%>
Output
:
Testing
Fund Without Class
// Strongly
Type View
public ActionResult Method3()
{
List<Fund>
objFunds = new List<Fund>
{
new Fund
{ FundID = 1, FundName = "USGlobal"},
new Fund
{ FundID = 2, FundName = "FBI Dealing"},
new Fund
{ FundID = 3, FundName = "Mirror Fund"}
};
ViewData["products"] =
objFunds;
return
View();
}
HTML
<% foreach (var item in (List<Fund>)ViewData["products"])
{ %>
<li> <%= item.FundID %> <%= item.FundName %> </li>
<% } %>
OUTPUT
·
1
USGlobal
·
2
FBI Dealing
·
3
Mirror Fund
// Example to use View Model or Pass Multiple business entities to View
public ActionResult
Method4()
{
List<Fund>
objFunds = new List<Fund>
{
new Fund
{ FundID = 1, FundName = "USGlobal"},
new Fund
{ FundID = 2, FundName = "FBI Dealing"},
new Fund
{ FundID = 3, FundName = "Mirror Fund"}
};
List<Price>
objPrice= new List<Price>
{
new Price
{ PriceID = 1, FundPrice = "22222"},
new Price
{ PriceID = 2, FundPrice = "33333"},
new Price
{ PriceID = 3, FundPrice = "44444"}
};
return View(new ProductViewModel(objFunds, objPrice));
}
HTML
Note: It is good to practice to defining the view model in the controller class. The view
model is directly related to the controller actions or you can make another
class with View Mode.
<%@ Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<MvcTextApplication.Controllers.ExperimentsController+ProductViewModel>"
%>
<h2>Funds</h2>
<% foreach (var item in Model.Fund)
{ %>
<li> <%= item.FundID %> <%= item.FundName
%> </li>
<% } %>
<hr />
<h2>Fund prices</h2>
<% foreach (var item in Model.Price )
{ %>
<li> <%= item.PriceID %> <%= item.FundPrice
%> </li>
<% } %>
OUTPUT
}
}
No comments :
Post a Comment