0

I have a model that include a list of another model.Now,I try use it in partial view page as a dropdownlistfor.I don't want use viewbag or selectlist.Do I need to write a helper ?

Product Model

    [Key]
    public int Id { get; set; }

    public int? Code { get; set; }

    public int? ProductGroupId { get; set; }

    public virtual ProductGroup ProductGroup { get; set; }

    public List<ProductGroup> ProductGroups { get; set; } 

ProductGroup Model

   [Key]
   public int Id { get; set; }


   public int? THoldingAdminsId { get; set; }

   public virtual List<Product> Products { get; set; }


   public int? Code { get; set; }



   public string CName { get; set; }

I want display a list of productgroup in product form as a dropdownlistfor. any help ?

Motion
  • 116
  • 1
  • 11
  • To which Model, your view is strongly bound? – mmushtaq Jul 19 '16 at 06:44
  • I want some thing like this : @html.dropdownlistfor(model=>model.ProductGroupId,Model.PrpductGroups) – Motion Jul 19 '16 at 06:47
  • I know you want something like this. But If your View is strongly bound to **ProductGroup** model then you can't assign the property of other model **Product** in View. – mmushtaq Jul 19 '16 at 06:51
  • oh no,I use this in view : @model model.entity.Product – Motion Jul 19 '16 at 06:57
  • The `DropDownListFor()` method accepts `IEnumerable` as the 2nd parameter, so its `@Html.DropDownListFor(m => m.ProductGroupId, new SelectList(Model.ProductGroups, "Id ", "CName "))`. Why are you saying you do not want to use `SelectList`? –  Jul 19 '16 at 07:00
  • I mean exactly this was,I don't knew that should be cast it.Thanks. – Motion Jul 19 '16 at 07:05
  • 1
    What you should be doing is using a view model with property `IEnumerable ProductGroups`. You should not use a data model in a view when editing - [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Jul 19 '16 at 07:11
  • @StephenMuecke Thank u so much – Motion Jul 19 '16 at 11:48

0 Answers0