Access List value from ViewData in MVC

Let’s create a mvc application and name it mvcDemo.

Let’s create a model and name it StudentModel.

public class StudentModel
{
     
   public int StudentId { get; set; }

   public string Name { get; set; }

   public bool IsRequired { get; set; }

   public string Address { get; set; }

   public string City { get; set; }

}

In HomeController.cs

public ActionResult HtmlView()
{
List objSt = new List();
StudentModel s = new StudentModel();
       s.Name = "Pramod";
       s.IsRequired = false;
       s.Address = "Shalimar Garden";
       s.City = "Ghaziabad";
       objSt.Add(s);

       StudentModel s1 = new StudentModel();
       s1.Name = "Anuj";
       s1.IsRequired = true;
       s1.Address = "New Delhi";
       s1.City = "New Delhi";
       objSt.Add(s1);
       ViewData["Data"] = objSt;
return View();
}

Let’s add a view and name it HtmlView.

2
Press F5 and run the application and see the output.

Output

1
I hope you enjoy this article.
Happy coding. :)

Tags: , , , , ,