How we can set value of View beg in asp.net mvc?

  By Ahsan   Posted on June-24-2022 

How to set and get value from TempData in asp.net mvc?

In Asp.Net mvc we use a term Temp data which means a short living instance, A temp data is used to transfer data from view to controller, controller to view, or controller to controller and on action method to another action method. It is used during present and subsequent request only.it keeps information as long as the HTTP request is to be active. In sample words we can say that temp data store data temporally and remove it after retrieving values.

 

 

How we can set value of View beg in asp.net mvc?

In Asp.Net mvc view bag is used to transfer data from controller to the view. It is dynamically type property. In simple word it is the data holder which enable the definition of the dynamic pass data controller to view. There some variable that known in program that values entered during run time, you can put just anything you want in view bag. It is the great way to access data that you use but may reside outside the data model. It commonly used in shopping cart, dropdown list option to bind, widgets etc. following figure illustrate the View bag.

 

 

Viewbeg.Name=”Ahsan”

 

@Viewdata.Name

 

           
     
   
     
 
 

 

 

            (Controller)                                                                 (View)

Above figure illustrate that a string name is assigned to view beg name as “Name”. On the other side on view, view beg call as mentioned.

Following are the example are mentioned below demonstrate the View beg.

    public class StudentController : Controller

    {

        IList<Student> studentList = new List<Student>() {

        new Student(){ StudentID=1, StudentName="Ali", Age = 21 },

        new Student(){ StudentID=2, StudentName="Sohail", Age = 25 },

        new Student(){ StudentID=3, StudentName="Waqas", Age = 20 },

        new Student(){ StudentID=4, StudentName="Ahsan", Age = 31 },

        new Student(){ StudentID=5, StudentName="Ahtasham", Age = 19 }

                };

        // GET: Student

        public ActionResult Index()

        {

            ViewBag.TotalStudents = studentList.Count();

 

            return View();

        }

 

    }

 

The above example show that the first we have a list of student with new object studentlist. After that we have add data in list. Next we have an action method name as Index in which we explain our view bag. In view bag we first write the Viewbag the write the Dot and then name of view bag through which we access then we assign the object of student list in which all the collection of the data. In this example we attach the count function with object so that we can count the number of data in list.

Now we understand how we can access the view bag in view. Below example


 
<label>Total Students:</label>  @ViewBag.TotalStudents

 
In above mentioned code we just write the view bag and write view bag name which is given in controller. On that way all the data transfer from controller to the view.

 
This example show how assign integer to view beg:
In controller:

ViewBag.salary = 1400;

In view:

                    int Salary=(int)ViewBag.salary;

 

This example show how assign string to view beg:
In controller:
                    ViewBag.MyString = “Hello”;
In View:
                    <p>@ViewBag.MyString</p>

 

 
Result/Conclusion:
  1. The view bag is used to send or transfer data from Controller to view with dynamic property, and not need to typecast for complex data types.
  2. In controller you can write as the view bed:

 
               ViewBag.Name_of_viewbag = value_or_list_or_any_object_assign_to_view_bag;

 
  1. In view side we can describe as the view bag as:
   
 @ViewBag.Name_of_viewbag
  1. It does not typecast value while retrieving data.
  2. It can trough run time expectation if wrong method used.
  3. View bag property name must match with controller name,

 

 

 

@Tempdata[“Name”]

 

           
   
     
     
 
 

 

 

            (Controller)                                                                 (View)

 

Name=Tempdata[“Name”]

@{

TempData["Name"]="Ahsan";

}

 

           
     
     
   
 
 

 

 

 

 

            (Controller)                                                                 (View)

 

Name=Tempdata[“Name”]

Tempdata[“Name”]=”Ahsan”

 

           
     
   
     
 

 

 

 

 (Controller)                                                                (Controller)

 

The below example is show the transfer data from action to action method using Tem data.

public class TempController : Controller

{

    public ActionResult Index()

    {

        TempData["name"] = "Ahsan";

 

        return View();

    }

 

    public ActionResult About()

    {

        string name;

       

        if(TempData.ContainsKey("name"))

            name = TempData["name"].ToString();

 

        return View();

    }

 

   

}

 

In this example we have two action method name as Index and about. In index action method we define the temp data with name “Name” and assign value “Ahsan”. Its means that we have an temp data with value. In About action method we have string type variable and then we access the temp data by assign this variable. Temp data name “Name” id assign there value to string variable.

Now we take another example which transfer data from controller to view.

public class TempController: Controller
{
    public ActionResult Index()
    {
        TempData["Name"] = "Ahsan";

 
        return View();
    }

 
    
}

 

In this example we have one action method name as Index. In index action method we define the temp data with name “Name” and assign value “Ahsan”. Its means that we have an temp data with value. Now we can see how to access this in view:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}

 
<lebel>My name is @TempData["Name"] </lebel>
 
For accessing the temp data in view we firstly write the sign “@”  then write the tempdata with square brackets enter name of temp data.

 
This example show how to pass list with the help of object using temp data.

 

 
List<Employee> emp = new List<Employee> 
{
 new Employee 
{
 EmployeeId = 1, 
EmployeeName = "John",
 Address = "12 Fremont St. Clermont, FL 2813", 
Phone = "+1-234-2838421" 
}
};

 
TempData["employee"] = emp;    
return View(); 

 
This example show how to pass integer using temp data.

 
In controller:

 
public ActionResult Form()
{
    TempData["sform"] = 5;
    return View();
}
In view:

 
               <p> @TempData["sform"]</p>
Result/Conclusion:
  1. The temp data is used to transfer data from Controller to View and view to controller also action method to another action method.
  2. For declaring action method we use following syntax:

 

  TempData["name"] = "Ahsan";

 

  1. For transfer data on action method to another action method means with in controller we use following syntax:

 

        name = TempData["name"].ToString();

 

  1. For show data in View we use following syntax:

 

  @TempData["Name"]

By      24-Jun-2022 Views  80



Comments


Your Comments

You may also read following recent articles