Home > Enterprise >  How to use List or IList in place of IEnumerable in Asp.NET MVC Views name spaces?
How to use List or IList in place of IEnumerable in Asp.NET MVC Views name spaces?

Time:12-05

My understanding about IList is it implements both IEnumerable and ICollection. and List is a concrete implementation of IList interface.

So I had used IEnumerable in the view namespaces many times to iterate over model objects. But when using IList or List in the namespaces, I'm getting an

Server Error in '/' Application

So,

  1. Why is this happening? and
  2. If I want to use List or IList what should be done?

Action in Controller :

private DummyProjectContext db = new DummyProjectContext();
  // GET: BankAccounts
    public ActionResult Index()
    {
        List<BankAccount> ba = db.BankAccounts.ToList();
        return View(ba);
    }

Index.cshtml :

@model IList<DummyProject.Models.BankAccount>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Amount)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Amount)
        </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>

Well, I know there IEnumerable is sufficient for this example and that should be used. I want to know why using IList or List , I'm getting this error?

CodePudding user response:

I Created Asp.net Project And Added a Dummy List But I Have not gotten that error Maybe Your list Object Is Null.

Controller:

var s=new List<string>();
s.add("test");
return view(s);

view:
@model IList<string>

<ul>
  @foreach(var item in Model)
  {
   <li>item</li>
  }
</ul>

For More trouble shooting: Please copy all the stack trace of an error when You run project for seeing complete error. It maybe occurred for other reason.

CodePudding user response:

Your code in the question contains two errors and doesn't compiling:

@Html.DisplayNameFor(model => model.Name)
...

@Html.DisplayNameFor(model => model.Amount)
...

You have to fix these errors.

CodePudding user response:

for your first Q: this is a rout mapping for your URL in your project

 routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

now this error happening when you web app cant find your rout of project I Recommended to you study more about routing in asp -MVC check out this link: https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/asp-net-mvc-routing-overview-cs

for your second Q : System.Collections.Generic

this name space is for Collections Generics now you need study more about Generics and Difference of them and after them u can choose which one of them can help you check out this link:

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic?view=net-6.0

and this

https://medium.com/developers-arena/ienumerable-vs-icollection-vs-ilist-vs-iqueryable-in-c-2101351453db

  • Related