Murugan.com
Murugan Andezuthu Dharmaratnam

  |  HOME   |  BLOG   |  TWITTER   |  ARTICLES   |  8086  |  C++   |  VC++   |  ASP .NET   |  VB .NET   |  JAVA SCRIPT   |  MS SQL   |  PHP   |  MY   |  VIDEOS   |  DOWNLOADS   |  CONTACT ME   |  



asp net mvc passing data between controllers views and action methods


Home  > ASP.NET  > MVC  > asp net mvc passing data between controllers views and action methods 
       
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()


index

mvc combobox ajax

What is MVC

MVC design pattern

Advantages of an MVC Based Web Application

Advantages of a Web Forms Based Web Application

Features of the ASP.NET MVC Framework

common used mvc namespace or mvc classes

mvc ActionLink return value from a textbox

MVC What does controller do

mvc how http request is processed

mvc Action Methods

MVC Return Types of mvc function or Action Retrun Types of an Action Method

mvc NonActionAttribute

MVC Partial Views

Helper Classes and Members for Rendering Views

What is MVC ViewDataDictionary

What is MVC Model

MVC Routing

MVC Helper Methods

mvc ActionLink

mvc routeValues

mvc what is reflection

asp net mvc passing data between controllers views and action methods

Create Mvc view by rendering data from differenet action methods

asp.net mvc dropdownlist example

How to publish MVC Application on web

mvc dropdownlist get selected text

asp .net mvc populate a dropdownlist from DataSet

asp .net mvc create a list selectlist from DataSet

asp .net mvc select options dropdownlist

Works on local machine System.ArgumentException Format of the initialization string does not conform to specification starting at index

mvc session timeout

iis mvc displays directory list and not application

iis mvc HTTP Error 50019 Internal Server Error section is locked at a parent level overrideMode

iis mvc error Could not load file or assembly System.Web.Mvc Version 2.0.0.0

SETTING UP THE DATABASE FOR MEMBERSHIP AUTHENTICATION IN MVC

One or more validation errors were detected during model generation

mvc authorize attribute does not store persist post data

asp .net mvc captcha

mvc Class Persistance Postback

Charts & Graphs Jquery plugin

Html.EditorFor Adding HTML Attributes



  |  HOME   |  BLOG   |  TWITTER   |  ARTICLES   |  8086  |  C++   |  VC++   |  ASP .NET   |  VB .NET   |  JAVA SCRIPT   |  MS SQL   |  PHP   |  MY   |  VIDEOS   |  DOWNLOADS   |  CONTACT ME   |  

Copyright 2009 @ Murugan Andezuthu Dharmaratnam