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<StudentModel> objSt = new List<StudentModel>();
- 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.
- @using MvcDemo.Models;
- @{
- ViewBag.Title = "HtmlView";
- }
-
- <h2 style="font-family:Verdana;">HtmlView</h2>
- <div style="font-family:Verdana; font-size:13px; border:dashed 1px black; width:350px;">
- <table>
- <tr>
- <td style="font-family:Verdana; font-size:14px; font-weight:bold;">Name</td>
- <td style="font-family:Verdana; font-size:14px; font-weight:bold;">Address</td>
- </tr>
- @{
- if(ViewData["Data"]!=null)
- {
- if(ViewData.Values !=null && ViewData.Values.Count()>0)
- {
- foreach (var item in ViewData["Data"] as List<StudentModel>)
- {
- <tr>
- <td style="font-family:Verdana; font-size:12px;">@item.Name</td>
- <td style="font-family:Verdana; font-size:12px;">@item.Address</td>
- </tr>
- }
- }
- }
- }
- </table>
- </div>
Press F5 and run the application and see the output.
Output
I hope you enjoy this article.
Happy coding.