Jesna
Jesna

Reputation: 55

How to Convert an IEnumerable model object to single model object in view , MVC3

This is not a duplicate.Though the other question is same as this it got solved when it deviated from the procedure. Here i again stumbled upon the same question.

Iam using a DB First approach. I have a context file called Dynaportal.context.cs, which has the class called DynaPortalEntities:-

public partial class DynaPortalEntities : DbContext
{
   ...    
    public DbSet<Page> Pages{ get; set; }

    public DbSet<TemplateMaster> TemplateMasters { get; set; }
}

In view

@model DynaPortalMVC.Models.DynaPortalEntities 

and in a foreach loop

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

In the same view , I need a page model which is not iEnumerable, like this:-

@Html.EditorFor(model => model.Pages.Title)===========>This shows error under Title

So here i should convert the IEnumerable Model.Pages to a single page object to get model.pages.Title.

Upvotes: 0

Views: 1697

Answers (1)

Davin Tryon
Davin Tryon

Reputation: 67316

Yes, if I understand correctly, Razor does not know what Page you want the title for since you are asking for the Title of all Pages.

Usually, you would loop through the Pages and output each title using @foreach. Or, index into the Pages: @Html.EditorFor(model => model.Pages.First().Title).

Upvotes: 1

Related Questions