1

I am trying to render my views using DisplayForModel and EditorForModel helper function. As the first step, I have the list controller that writtens Ienumarable list of organizations.

I have followed the answers of Darian from this SO question, and did as below.

In - list.cshtml

@using PartyBiz.Models.Objects
@model IEnumerable<Organization>

@Html.DisplayFor( model => model.Organizations )

In - Organization.cshtml

@model PartyBiz.Models.Objects.Organization

<div>@Model.Caption </div>

<div>@Model.Description  </div>

<div>@Model.NameInUse  </div>

My model has the below property

public IEnumerable<Organization> Organizations { get; set; }

Controller list action

public ViewResult List(OrganizationQuery qry = null)
{
    return View(OrganizationRepo.All() as IEnumerable<Organization>);
}

However I am getting the compile error:

Compiler Error Message: CS1061: 'System.Collections.Generic.IEnumerable' does not contain a definition for 'Organizations' and no extension method 'Organizations' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)

please advise what I am doing wrong here?

Community
  • 1
  • 1
mmssaann
  • 1,507
  • 6
  • 27
  • 55

2 Answers2

0

The problem is in List.cshtml. You need to change it to soemthing like bellow:

@using PartyBiz.Models.Objects @model IEnumerable

@foreach(var m in Model)
{
    @Html.DisplayFor( m => m.Organizations )
}
Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
0

Since your model is already an IEnumerable, what you really need is

@Html.DisplayForModel()

or

@Html.DisplayForModel("Organization") //explicitly provide template name

The question you have referenced assumed that there was another model class with property Customers that could be enumerated. Your situation is a bit different.

Andrei
  • 55,890
  • 9
  • 87
  • 108
  • this will display proxy entities as below:System.Data.Entity.DynamicProxies.Party_A9AAEA1B13CDAAAE6 - there is some long text ofcourse – mmssaann Jun 24 '13 at 12:18
  • @mmssddff, then you can try `DispalyForModel` - please see the update. – Andrei Jun 24 '13 at 12:25
  • DisplayForModel will work only for single organization, I am not able to show list of organizations using DisplayForModel... – mmssaann Jun 25 '13 at 05:26
  • @mmssddff, have you tried it? For all I know it should work provided your model is `IEnumerable` and you have a template defined for single `Organization`. – Andrei Jun 25 '13 at 07:08