MVC Nested Model Collection

panais

I am new to MVC and I am trying to display information from a nested model collection on the page.

so my model is as follow:

public partial class Parent
{
    public Parent()
    {
        this.Childs = new HashSet<Child>();
     }

    public int ParentID { get; set; } 
     public string Name { get; set; } 

    public virtual ICollection<Child> Childs { get; set; }
 }

To display the information on the Parent view i used :

@foreach (Child c in Model.Childs)
{
    @c.Name 
}

The above works but i would like to use a different view for the childs so i have tried the following:

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

and defined the following view:

@model WebApplication1.Models.Child

<div>
      @Html.DisplayFor(model => model.Name)
</div>

This doesn't work and what I am getting is a System.Data.Entity.DynamicProxies displayed instead of the list of child names. I have read that this is because MVC5 doesn't know what view to use. I have tried specifying the view in the Display for as @Html.DisplayFor(model => model.Childs, "ViewName1.cshtml") but that didn't help at all.

In addition to the above I would like to use something similar for @Html.EditorFor(model => model.Childs).

dom

Start by creating a DisplayTemplates sub-folder in your Parent view folder. Then create a view in that folder called Child.cshtml.

Child.cshtml

@model WebApplication1.Models.Child

@Model.Name
...more HTML markup if needed

Then all you do is call @Html.DisplayFor(m => m.Childs) and the framework will do the rest for you. Note that if you use the overload that lets you specify which view to use, the loop will not be done automatically for you.

Repeat the same process for editor templates, with a EditorTemplates folder and following the same conventions (view name = type name) for naming your views.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MVC Nested Model Collection

From Dev

MVC Nested View Model with Validation

From Dev

MVC Model with inherited nested class

From Dev

Nested Collection with KnockoutJs and Asp.net MVC

From Dev

MVC5 model collection data type

From Dev

MVC/AJAX - sent current Model collection to controller

From Dev

MVC - Sending collection View Model using AutoMapper

From Dev

How should I model my MongoDB collection for nested documents?

From Dev

Backbone.js How to retrieve a model in a nested collection?

From Dev

ASP.NET MVC nested collection with create view

From Dev

Update a Model and a Collection of Models on a Single View (MVC4)

From Dev

MVC Form Model returning null for complex object collection

From Dev

ASP.NET MVC Model Binding with jQuery ajax request for collection

From Dev

MVC post model- Collection property has no elements

From Dev

MVC handling Collection Item Index in Partial view model

From Dev

Post one item from model collection to controller method in MVC

From Dev

Webgrid rows based on a collection in Model. MVC4

From Dev

MVC post model- Collection property has no elements

From Dev

ASP.NET MVC/Web API model binding item to collection

From Dev

Post one item from model collection to controller method in MVC

From Dev

Implement a collection of an object in my view model - ASP.net MVC

From Dev

Accessing child collection in MVC5 razow views from model

From Dev

Validation of nested models in view model in ASP.Net MVC

From Dev

ASP.NET MVC Model Binding Issue with Nested Collections

From Dev

MVC Complex model binding with inheritance, nested viewModels and partial views

From Dev

Problems model binding nested list in ASP.NET MVC

From Dev

Nested objects in view model confuses MVC 4 routing?

From Dev

Problems model binding nested list in ASP.NET MVC

From Dev

Nested objects in view model confuses MVC 4 routing?

Related Related

  1. 1

    MVC Nested Model Collection

  2. 2

    MVC Nested View Model with Validation

  3. 3

    MVC Model with inherited nested class

  4. 4

    Nested Collection with KnockoutJs and Asp.net MVC

  5. 5

    MVC5 model collection data type

  6. 6

    MVC/AJAX - sent current Model collection to controller

  7. 7

    MVC - Sending collection View Model using AutoMapper

  8. 8

    How should I model my MongoDB collection for nested documents?

  9. 9

    Backbone.js How to retrieve a model in a nested collection?

  10. 10

    ASP.NET MVC nested collection with create view

  11. 11

    Update a Model and a Collection of Models on a Single View (MVC4)

  12. 12

    MVC Form Model returning null for complex object collection

  13. 13

    ASP.NET MVC Model Binding with jQuery ajax request for collection

  14. 14

    MVC post model- Collection property has no elements

  15. 15

    MVC handling Collection Item Index in Partial view model

  16. 16

    Post one item from model collection to controller method in MVC

  17. 17

    Webgrid rows based on a collection in Model. MVC4

  18. 18

    MVC post model- Collection property has no elements

  19. 19

    ASP.NET MVC/Web API model binding item to collection

  20. 20

    Post one item from model collection to controller method in MVC

  21. 21

    Implement a collection of an object in my view model - ASP.net MVC

  22. 22

    Accessing child collection in MVC5 razow views from model

  23. 23

    Validation of nested models in view model in ASP.Net MVC

  24. 24

    ASP.NET MVC Model Binding Issue with Nested Collections

  25. 25

    MVC Complex model binding with inheritance, nested viewModels and partial views

  26. 26

    Problems model binding nested list in ASP.NET MVC

  27. 27

    Nested objects in view model confuses MVC 4 routing?

  28. 28

    Problems model binding nested list in ASP.NET MVC

  29. 29

    Nested objects in view model confuses MVC 4 routing?

HotTag

Archive