This is driving me crazy, and I have studied the related posts on StackOverflow.
Basically I have 2 tables, Album and Genre with the former having a foreign key to Genre. I have modelled this using Model First in EF5, and generated the context and class files using MS’s T4 generator. Thus my Domains Class code is:
{
using System;
using System.Collections.Generic;
public partial class Album
{
public int Id { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public string AlbumArtUrl { get; set; }
public int GenreId {get; set;}
public virtual Genre Genre { get; set; }
}
}
You will notice that I have added :
public int GenreId {get; set;}
as per guidance inorder to enable the scaffolding to create the dropdowns in the Views and “SelectList” code in the controller. But it does not! However it does create, in the view:
<div class="editor-label">
@Html.LabelFor(model => model.GenreId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.GenreId)
@Html.ValidationMessageFor(model => model.GenreId)
</div>
So I am really puzzled. Any ideas would be greatly appreciated.
Thanks,
Ed
P.S. I am running VS2010 SP1 with ASP.NET MVC 3 Tools Update it seems, since I tried to install it and it said that it was already installed.
yee … aah!!! I have got it working with the MS Scaffold. Now I need to figure out how. I believe the issue was with the fact that I am using Model First and I need to ensure that I add the extra foreign key property into the model and not just into the Domain Class code, it needs to be in the EDMX. However this creates another problem in that 2 fields are now creates in SQL Server, one from the Navigation property and the other from this field. I may need to play with the mapping. I will
Controller code generated:
Associated View Code:
This is what I wanted. Obviously if anyone wishes to make comment on this then great, but it does work.
Thanks.