Can someone please help me out. I am trying to retrieve a list of storage details from the database and simply display the list in a view.
Storage Model:
public class StorageModel
{
[Required]
[Display(Name = "Storage Name")]
public string Name { get; set; }
[Required]
[Display(Name = "Date From")]
public string DateFrom { get; set; }
[Required]
[Display(Name = "Date To")]
public string DateTo { get; set; }
[Required]
[Display(Name = "Size")]
public string Size { get; set; }
}
Controller Method:
public ActionResult ViewStorage()
{
List<CommonLayer.TblNewsStorage> storageList = new BusinessLayer.Storage().getAllStorage().ToList();
return View(storageList);
}
Data being retrieved from the BusinessLayer above:
public IQueryable<CommonLayer.TblNewsStorage> getAllStorage()
{
return this.Entities.TblNewsStorage;
}
Now I created a strongly typed view with the StorageModel using view scaffold template, however it is not working. What exactly am I doing wrong? I tried passing a var
instead of a List
but still it is not working. I am new to MVC so I must be doing something wrong. Which is the proper way to pass and display a list of data to a view?
View code generated by MVC:
@model IEnumerable<NewsLibrary.Models.StorageModel>
@{
ViewBag.Title = "ViewStorage";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>ViewStorage</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.DateFrom)
</th>
<th>
@Html.DisplayNameFor(model => model.DateTo)
</th>
<th>
@Html.DisplayNameFor(model => model.Size)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateFrom)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateTo)
</td>
<td>
@Html.DisplayFor(modelItem => item.Size)
</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>
}
</table>
I get the following error:
The model item passed into the dictionary is of type 'System.Collections.Generic.List
1[CommonLayer.TblNewsStorage]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[NewsLibrary.Models.StorageModel]'.