Let’s create a MVC application and name it MVCDemo.

Let’s create a model and name it StudentModel.

  1. public class StudentModel
  2. {
  3. public int StudentId { get; set; }
  4. public string Name { get; set; }
  5. public bool IsRequired { get; set; }
  6. public string Address { get; set; }
  7. public string City { get; set; }
  8. }
In HomeController.cs
  1. public ActionResult HtmlView()
  2. {
  3. List<StudentModel> objSt = new List<StudentModel>();
  4. StudentModel s = new StudentModel();
  5. s.Name = "Pramod";
  6. s.IsRequired = false;
  7. s.Address = "Shalimar Garden";
  8. s.City = "Ghaziabad";
  9. objSt.Add(s);
  10. StudentModel s1 = new StudentModel();
  11. s1.Name = "Anuj";
  12. s1.IsRequired = true;
  13. s1.Address = "New Delhi";
  14. s1.City = "New Delhi";
  15. objSt.Add(s1);
  16. ViewData["Data"] = objSt;
  17. return View();
  18. }
Let’s add a view and name it HtmlView.
  1. @using MvcDemo.Models;
  2. @{
  3. ViewBag.Title = "HtmlView";
  4. }
  5. <h2 style="font-family:Verdana;">HtmlView</h2>
  6. <div style="font-family:Verdana; font-size:13px; border:dashed 1px black; width:350px;">
  7. <table>
  8. <tr>
  9. <td style="font-family:Verdana; font-size:14px; font-weight:bold;">Name</td>
  10. <td style="font-family:Verdana; font-size:14px; font-weight:bold;">Address</td>
  11. </tr>
  12. @{
  13. if(ViewData["Data"]!=null)
  14. {
  15. if(ViewData.Values !=null && ViewData.Values.Count()>0)
  16. {
  17. foreach (var item in ViewData["Data"] as List<StudentModel>)
  18. {
  19. <tr>
  20. <td style="font-family:Verdana; font-size:12px;">@item.Name</td>
  21. <td style="font-family:Verdana; font-size:12px;">@item.Address</td>
  22. </tr>
  23. }
  24. }
  25. }
  26. }
  27. </table>
  28. </div>
Press F5 and run the application and see the output.

Output

html view

I hope you enjoy this article.
Happy coding.