.net mvc razor dropdowns for foreign keys

我的梦境 提交于 2019-12-07 13:42:31

Easiest way is to define a scalar property named CategoryId in your SubCategory class

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

    public int CategoryID { get; set; }
    public Category category { get; set; }

    [StringLength(255, MinimumLength = 1)]
    public string Name { get; set; }
}

Then correctly map it in EF

After that you put

@Html.DropDownListFor(
    model => model.CategoryID, category SelectList instance)
)

Other option is to create a custom model binder to bind Category type.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!