locked
how to displaying the list record in mvc? RRS feed

  • Question

  • User1421057020 posted

    I want to displaying the list record?

    I want to displaying the list record but I am facing error:

    student.cs

        public class Employee
        {
            public int Id { get; set; }
    
            public string Name { get; set; }
    
            public string Designaton { get; set; }
    
        }
    

    HomeController.cs

            public ActionResult Index()
            {
                List<Employee> emplist = new List<Employee>() {
                        new Employee(){ Id=1, Name="Steve", Designaton = "hr" },
                        new Employee(){ Id=2, Name="Bill", Designaton = "finance" },
                        new Employee(){ Id=3, Name="Ram", Designaton = "hr" },
                        new Employee(){ Id=4, Name="Ron", Designaton = "finance" },
                        new Employee(){ Id=5, Name="Rob", Designaton = "finance" }
                    };
    
                ViewBag.totalemp = emplist.Count();
    
                return View();
            }

    Index.cshtml

    @*@model migrationdemo.Models.Employee*@
    
    @{
        ViewBag.Title = "Index";
        //var employee = ViewBag.emp;
    
        //var getlist = ViewBag.totalemp as IEnumerable<migrationdemo.Models.Employee>;
        //IEnumerable<migrationdemo.Models.Employee> personlist = ViewBag.totalemp as IEnumerable<migrationdemo.Models.Employee>;
    }
    
    <h2>Index</h2>
    <td>@((ViewBag.totalemp as IEnumerable<migrationdemo.Models.Employee>))</td>
    @foreach (var item in ViewBag.totalemp) {     @item.Id     @item.Name     @item.Designaton } @*@if (ViewBag.totalemp != null)     {         foreach (var item in (ViewBag.totalemp as List<migrationdemo.Models.Employee>))         {             @item.Id             @item.Name             @item.Designaton         }     }*@

    why list data is not display?

    Is there casting error? how to solve this issue?

    Sunday, June 7, 2020 6:31 AM

Answers

  • User634495354 posted

    Hi,raju bhai

    In  controller, you give the count of list to viewbag , the value of it is 5 ,not id name and designaton.

    You just need to change 

    ViewBag.totalemp = emplist;

    Best regards.

    Bruce12138

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 8, 2020 1:43 AM

All replies

  • User475983607 posted

    You've assigned an integer to ViewBag.totalemp.

    ViewBag.totalemp = emplist.Count();

    In the View, you attempt to loop over the integer.

    @foreach (var item in ViewBag.totalemp)
    {
        @item.Id
        @item.Name
        @item.Designaton
    }

    There are several approaches.  Rather than assigning the integer assign the results set to the ViewBag.

    Pass the model to the View as illustrated in every beginning level tutorial. 

    Use Visual Studio to scaffold a new List View which will create the everything you need.  This is the best option IMHO given your experience level.  Delete the current View.  Then right click the Controller Action and select "Add View" to create a new View.  In the "Add MVC View" dialog box select "List" as the template.  Select the model from the "Model class" option and click the "Add" button.  

            public ActionResult Index()
            {
                List<Employee> emplist = new List<Employee>() {
                        new Employee(){ Id=1, Name="Steve", Designaton = "hr" },
                        new Employee(){ Id=2, Name="Bill", Designaton = "finance" },
                        new Employee(){ Id=3, Name="Ram", Designaton = "hr" },
                        new Employee(){ Id=4, Name="Ron", Designaton = "finance" },
                        new Employee(){ Id=5, Name="Rob", Designaton = "finance" }
                    };
    
                return View(emplist);
            }
    @model IEnumerable<MvcBasic.Models.Employee>
    
    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    
    <p>
        @Html.ActionLink("Create", "Create")
    </p>
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Id)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Name)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Designaton)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
    @foreach (var item in Model) {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Designaton)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                    @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                    @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
                </td>
            </tr>
    }
        </tbody>
    </table>
    

    Scaffold with Entity Framework

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/generating-views

    I recommend that you go through a few beginning level tutorials before moving forward.

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/getting-started

    Sunday, June 7, 2020 11:09 AM
  • User634495354 posted

    Hi,raju bhai

    In  controller, you give the count of list to viewbag , the value of it is 5 ,not id name and designaton.

    You just need to change 

    ViewBag.totalemp = emplist;

    Best regards.

    Bruce12138

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 8, 2020 1:43 AM
  • User1421057020 posted

    Yes that is work also 

    when count is return the 5 that is the reason error occur  

    just pass the emplist and issue is solved

    thanks for the helpBruce12138 and mgebhard

    Monday, June 8, 2020 4:40 AM
  • User634495354 posted

    If you think my reply solves your problem,can you mark  it as answer. It is very useful and helpful to me.

    Thank you.

    Best regards.

    Burce12138

    Tuesday, June 23, 2020 2:55 AM