1

A lot of my Entity classes have other classes or collection of classes as properties.

For example:

public class SomeClass
{
    public int ID {get; set;}

    public string SomeValue {get; set;}

    public virtual ICollection<AnotherEntity> AnotherEntities {get; set;}

    public virtual SingleEntity {get; set;}
}

When I scaffold SomeClass in Visual Studio it does a great job of creating the controller and all the views for setting normal properties like SomeValue but it doesn't add anything for making the associations between SomeClass and AnotherEntities / SingleEntity.

Is there a simple way of handling this? An addon perhaps? I have tried to search for examples but I feel I am lacking the proper terms to enter into Google. I imagine I should code some sort of popup or something but some simple examples would really make it easier.

Your advice is much appreciated.

Guerrilla
  • 13,375
  • 31
  • 109
  • 210
  • If the view is for creating/editing `SomeClass`, refer [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for dynamically adding/removing collection items –  Feb 13 '17 at 04:26
  • @StephenMuecke ok, I read through it a couple of times and assume the answer is to load a partial view with ajax. I am still finding it hard to grasp without an example. – Guerrilla Feb 13 '17 at 05:14
  • Refer [this answer](http://stackexchange.com/leagues/1/alltime/stackoverflow/2008-07-31?sort=reputationchange&page=29) for a more complete example using `BeginCollectionItem`. In that example the model is a collection but in your case your have a model containing a collection so the view becomes `foreach(var item in Model.AnotherEntities) { ..` and in the partial `@model AnotherEntity @using (Html.BeginCollectionItem("AnotherEntities"))` –  Feb 13 '17 at 05:20

0 Answers0