Entity Framework Code 1st & Razor Not Generating DropDowns for FK fields?

I don't know where you saw or read such things. Here are the default editor templates in ASP. NET MVC.

As you can see absolutely none of them generates any select elements except the one for a nullable boolean property.

Up vote 0 down vote favorite share g+ share fb share tw.

I'm using ASP. NET MVC3 along with Razor and Entity Framework Code First to create a new and very small application. From everything I've read and seen, when there is a FK relationship between classes and data, Razor should generate a dropdown (select) input field.

However when the code below runs, I get a textbox for the PartnerTypeId and PartnerSubTypeId. Not sure where my malfunction is. CLASSES public class Partner { public int Id { get; set; } Required MinLength(5) MaxLength(300) public String Name { get; set; } Required public int PartnerTypeId { get; set; } public PartnerType PartnerType { get; set; } public int PartnerSubTypeId { get; set; } public PartnerSubType PartnerSubType { get; set; } } public class PartnerType { public int Id { get; set; } Required MaxLength(40) public String Name { get; set; } Required public bool IsActive { get; set; } public virtual ICollection Partners { get; set; } } VIEW Partner @Html.

LabelFor(model => model. Name) @Html. EditorFor(model => model.

Name) @Html. ValidationMessageFor(model => model. Name) @Html.

LabelFor(model => model. PartnerTypeId) @Html. EditorFor(model => model.

PartnerTypeId) @Html. ValidationMessageFor(model => model. PartnerTypeId) @Html.

LabelFor(model => model. PartnerSubTypeId) @Html. EditorFor(model => model.

PartnerSubTypeId) @Html. ValidationMessageFor(model => model. PartnerSubTypeId) c# asp.

Net-mvc-3 razor entity-framework-4.1 link|improve this question edited Mar 20 at 21:45 asked Mar 20 at 18:00RSolberg13k33590 96% accept rate.

From everything I've read and seen, when there is a FK relationship between classes and data, Razor should generate a dropdown (select) input field. I don't know where you saw or read such things. Here are the default editor templates in ASP.

NET MVC. As you can see absolutely none of them generates any elements except the one for a nullable boolean property. Razor doesn't know anything about what an FK relationship means.

It doesn't know what a database means. Razor is a templating view engine that works with models and which supports standard editor and display templates (see article I have linked to) which are dependent on the metadata you decorate your model properties with and the type of the properties. If you are not satisfied with what the default templates has to offer you could, of course, always write your custom templates.

For example you could write an editor template that would generate a dropdown list. Of course in order to generate a dropdown list don't forget that you need 2 things: a scalar property to bind the selected value to and a collection property of some complex type where each element consists of at least 2 properties representing the value and text that will be used to bind the options of the dropdown list.

You could at least a leave a comment when downvoting or even better answer the question providing counter facts. – Darin Dimitrov Mar 20 at 22:02.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions