1

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.List1[CommonLayer.TblNewsStorage]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[NewsLibrary.Models.StorageModel]'.

wooters
  • 829
  • 6
  • 24
rikket
  • 2,357
  • 7
  • 46
  • 74

3 Answers3

2

The

@model IEnumerable<NewsLibrary.Models.StorageModel>

must have the same datatype passed in.

You are passing in

List<CommonLayer.TblNewsStorage>
Max
  • 3,280
  • 2
  • 26
  • 30
2

If you take a closer look at the Model the View expects you see it's IEnumerable of NewsLibrary.Models.StorageModel. You are passing a list/IEnumerable of the type CommonLayer.TblNewsStorage. Make sure these two are the same datatype.

  • The Model is based on the type CommonLayer.TblNewsStorage (retrieved directly from the database), can I do some sort of conversion? – rikket Apr 29 '14 at 15:58
  • use something like automapper to do the automatic mapping. – qamar Apr 29 '14 at 15:59
  • I should convert them in the Controller before passing them to the view, or make a special ViewModel for the view :). http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc – Steven Vanden Broucke Apr 29 '14 at 18:16
-1

@foreach (var item in Model) { item.Name }

try like this. You need to remove @html.displayfor(model => item.name) to only item.name inside your tags

qamar
  • 1,437
  • 1
  • 9
  • 12
  • No, DisplayName actually goes outside. That function returns the display name (aka variable name, if it has no DisplayName attribute) – Dan Apr 29 '14 at 15:56