boutique replica bags up ideas

the best replique rolex and prices here.

julia highlight 99j hair color 10a quality straight human hair lace front wigs 8 - 24 inches pre plucked hairline 13x4 inches lace front brazilian wig onlinefor sale

How to Display list of data in view in ASP.NET MVC?

Updated on     Kisan Patel

This tutorial will show you how to display list of data in view in ASP.NET MVC?

While you want to display list of data which are stored in database then you have to create a method to get your data in controller.

So here are the steps to follow to display the list

1) create a method or ActionResult for your view, get the list of your data pass it in the View.

Here is My model

    
public class Profile
{
        [Key]
        public int Id { get; set; }

        [Required(ErrorMessage = "First Name is Required")]
        [Display(Name = "FirstName")]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "Email ID is Required")]
        [DataType(DataType.EmailAddress, ErrorMessage = "Email ID is not valid.")]
        [Display(Name = "Email Id")]
        public string EmailId { get; set; }

        [Required(ErrorMessage = "Contact No is Required")]
        [Display(Name = "Contact No")]
        [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Entered Contact No format is not valid.")]
        public string ContactNo { get; set; }

        [StringLength(5)]
        [Required(ErrorMessage = "Rating is Required")]
        public string Rating { get; set; }

        [Required(ErrorMessage = "Age is Required")]
        [Range(1, 100, ErrorMessage = "Age must be between $1 and $100")]
        public decimal Age { get; set; }
}

2) then Create a controller and method to pass list of data to view.

[HttpGet]
public ActionResult DataList()
{
     var list = _dbContext.Profile.ToList();
     return View(list);
}

3) Next, Right Click on method » Add view » then popup will open. In Popup, Select Template as List then select your model which data you are displaying (Here Profile model) then click Add then your View will automatically generated. now Run your project and see the output.

Your View will be like this

@model IEnumerable<Project.Models.Profile>

@{
 Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>DataList</title>
</head>
<body>
    <p>
       @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
      <tr>
        <th>
          @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.EmailId)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.ContactNo)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.Rating)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.Age)
        </th>
        <th></th>
    </tr>
    @foreach (var item in Model) {
      <tr>
        <td>
           @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
           @Html.DisplayFor(modelItem => item.EmailId)
        </td>
        <td>
           @Html.DisplayFor(modelItem => item.ContactNo)
        </td>    
        <td>
           @Html.DisplayFor(modelItem => item.Rating)
        </td>
        <td>
           @Html.DisplayFor(modelItem => item.Age)
        </td>
        <td>
           @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
           @Html.ActionLink("Details", "Details", new { id=item.Id }) |
           @Html.ActionLink("Delete", "Delete", new { id=item.Id })
       </td>
   </tr>
 }
 </table>
</body>
</html>

Your Output will be Like this

5


ASP.NET MVC

Leave a Reply