|
How to pass data in asp net mvc between
-> Cotrollers & Views
-> Weekly Typed Data
-> Strongly Typed Data
-> Temporary State Data Between Action Methods
1. Passing Data Between Controllers & View
=======================================
Using ViewData (( Weekly Typed Data ))
---------------------------------------------------------------
-> To render a view, you call the View method of the controller. To pass data to the view, you use the ViewData property . U can pass many type string, list , int etc as Values of ViewData
-> If you call the View method without parameters), the controller object's ViewData property is passed to the view that has the same name as the action method.
->In the view page, you can access the ViewData property to obtain data that was passed to the view.
VIEW DATA PROPERTY
The ViewData property is a dictionary that supports an indexer that accepts dictionary keys.
Note : When you pass data between a view and a controller by using the ViewData property, the data is not strongly typed
2. Passing (( Strongly Typed Data))
-----------------------------------------------------------------
->If you want to pass strongly typed data, change the @ Page declaration of the view so that the view inherits from ViewPage<TModel> instead of from ViewPage, as shown in the following example:
<%@ Page Inherits="ViewPage<Product>" %>
ViewPage<TModel> is the strongly-typed version of ViewPage. The ViewData property of ViewPage<TModel> returns a ViewDataDictionary<TModel> object, which contains strongly typed data for the view based on a model. The model is a class that contains properties for each data item that you want to pass. (A simpler approach to creating a strongly typed view page is to use the Add View dialog box.)
The following example shows the definition of a typical viewmodel class
public class ViewModelCountry
{
public DataSet DataSetCountry { get; set; }
}
The Data can be of any type. In the above example I have used DataSet Type since it is for a grid.
It Can bye
public String Name { get; set; }
public String Age{ get; set; }
Etc.
2. Passing Data Between Action methods
============================================
-> Action Methods might have to pass data to another action.
-> An action method can store data in the controller's TempDataDictionary object before it calls the controller's RedirectToAction method to invoke the next action. The TempData property value is stored in session state. Any action method that is called after the TempDataDictionary value is set can get values from the object and then process or display them. The value of TempData persists until it is read or until the session times out. Persisting TempData in this way enables scenarios such as redirection, because the values in TempData are available beyond a single request.
The following example shows a data class that is used to trap an error and to transfer data between actions.
public class InsertError
{
public string ErrorMessage { get; set; }
public string OriginalFirstName { get; set; }
public string OriginalLastName { get; set; }
}
// CustomersController
public ActionResult InsertCustomer(string firstName, string lastName)
{
// Check for input errors.
if (String.IsNullOrEmpty(firstName) ||
String.IsNullOrEmpty(lastName))
{
InsertError error = new InsertError();
error.ErrorMessage = "Both names are required.";
error.OriginalFirstName = firstName;
error.OriginalLastName = lastName;
TempData["error"] = error;
return RedirectToAction("NewCustomer");
}
// No errors
// ...
return View();
}
public ActionResult NewCustomer()
{
InsertError err = TempData["error"] as InsertError;
if (err != null)
{
// If there is error data from the previous action, display it.
ViewData["FirstName"] = err.OriginalFirstName;
ViewData["LastName"] = err.OriginalLastName;
ViewData["ErrorMessage"] = err.ErrorMessage;
}
// ...
return View()
|