Home > Software engineering >  Send List Type to View from Controller
Send List Type to View from Controller

Time:12-26

Entities:

public partial class Institution
{
public int? Id { get; set; }

public string? District { get; set; }

public string? InstitutionCode { get; set; }

public string? InstitutionName { get; set; }

public string? DemolitionStatus { get; set; }

public string? ReinforcementStatus { get; set; }
}

Controller:

  public class HomeController : Controller
{
    _context c;

    public HomeController(_context c)
    {
        this.c = c;
    }

    public IActionResult Index()
    {
       var GetAll=c.Institutions.ToList();  
        return View(GetAll);
    }
    }

In View

@model List<GetAll>

   @foreach (var item in Model)
    {

     @item.InstitutionName
    }

Also it doesn't work: The type or namespace name 'GetAll' could not be found

Try with @model SolutionName.Entities or Ienumarable or adding namespace @using FBMv3.Controllers

What is the wrong or missing?

CodePudding user response:

You probably want:

@model List<Institution>

You might also need a using in your view with the namespace where Institution is.

Check out the documentation: enter image description here

So you should write the code like this @model IEnumerable<DotNet6MVC.Models.Institution> instead of @model List<GetAll>

Complete sample for you:

Controller:

public IActionResult Index()
        {


            var instituteList = new List<Institution>()
            {
                new Institution(){ Id =101,District = "Dis-A", InstitutionCode = "IC-1",InstitutionName = "IN-AAA",DemolitionStatus="YES",ReinforcementStatus = "T"},
                new Institution(){ Id =102,District = "Dis-B", InstitutionCode = "IC-2",InstitutionName = "IN-BBB",DemolitionStatus="NO",ReinforcementStatus = "F"},
                new Institution(){ Id =103,District = "Dis-C", InstitutionCode = "IC-3",InstitutionName = "IN-CCC",DemolitionStatus="NO",ReinforcementStatus = "T"},
                new Institution(){ Id =104,District = "Dis-D", InstitutionCode = "IC-4",InstitutionName = "IN-DDD",DemolitionStatus="NO",ReinforcementStatus = "T"},
                new Institution(){ Id =105,District = "Dis-E", InstitutionCode = "IC-5",InstitutionName = "IN-EEE",DemolitionStatus="YES",ReinforcementStatus = "T"},
               

            };
            return View(instituteList);
        }

Note: You could write your controller code as well. Like this:

        var GetAll=c.Institutions.ToList();  
        return View(GetAll)

View:

@model IEnumerable<DotNet6MVC.Models.Institution>

@{
    ViewData["Title"] = "Index";
}
<table >
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Id)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.District)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.InstitutionCode)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.InstitutionName)
            </th>
           
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.District)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.InstitutionCode)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.InstitutionName)
                </td>
                <td>
                    <a asp-action="EditMember"  asp-route-memberId="@item.Id">Details</a> |  <a asp-action="EditMember"  asp-route-memberId="@item.Id">Edit</a>
                </td>
            </tr>
        }
    </tbody>
</table>

Note: Be double confirm in your scenario, it would be @model IEnumerable<YourProjectName.YourClassFolder.Institution>. Because, your problem is here.

Output:

enter image description here

Note: If you still have any concern on this I would highly recommend you to check this official document.

  • Related