1 Replies - 3406 Views - Last Post: 09 October 2013 - 03:15 PM

#1 tieugiang94  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 160
  • Joined: 07-December 11

[ EF - MVC ] Use scaffold to genrate DropDownList

Posted 12 September 2013 - 05:52 AM

I have to model
public class Category
    {
        public int CategoryId { get; set; }
        public string Name { get; set; }
        public List<Product> Products { get; set; }
    }

public class Product{
public int ProductId { get; set; }
        public string Name { get; set; }
        public Category Category { get; set; }
}




How to modify this two classes to make Scaffold ( Visual STudio 2013 ) auto generate DropDownList in both Create and Edit View ?

P/s: I Don't want to define an property CategoryId in Product class.

Is This A Good Question/Topic? 0
  • +

Replies To: [ EF - MVC ] Use scaffold to genrate DropDownList

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: [ EF - MVC ] Use scaffold to genrate DropDownList

Posted 09 October 2013 - 03:15 PM

You can use the UIHint attribute to tell it to use a specific partial view for that property.

Under your Views/Shared directory you can add two new folders, one named EditorTemplates and one named DisplayTemplates.
In each folder create a new partial view and name it whatever you use as the UIHint value. The partial view in the DisplayTemplates folder will be used when your code uses the DisplayFor syntax and the partial view in the EditorTemplates folder will be used when you use the EditorFor syntax.

public class Product{
    public int ProductId { get; set; }
    public string Name { get; set; }
    [UIHint("CategoryList")] // You would name your partial views 'CategoryList'
    public Category Category { get; set; }
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1