MvcScaffolding one-many relationship

I am using MVC 4, EF 4.3 and the MVCScaffolding package.

I have the following model classes

 public class Product
{
    [Key]
    public int ID { get; set; }
    [Required]
    public string Name { get; set; }
    public virtual Category Category { get; set; }
}
public class Category
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}

I derived the controllers as follows:

Scaffold Controller Category -Force 
Scaffold Controller Product -Force 

This created controllers / views, etc.

Per post by Steve Sanderson I would have thought that for _CreateOrEdit.cshtml for the Product there would be a drop down menu for the category, but it is not.

Followng is the content of _CreateOrEdit.cshtml and it does not contain an HTML template for the category

<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
   @Html.EditorFor(model => model.Name)
   @Html.ValidationMessageFor(model => model.Name)
</div>

What am I doing wrong?

+3
source share
1 answer

I think you also need to have the CategoryID property in your Product class. It will not be virtual because you want EntityFramework to save it in DB as a foreign key.

, , . , , .

, ID , , [Key].

+3

All Articles