Strongly-Typed ASP.NET MVC with ADO.NET Entity Framework [closed]

I’ve begun working with ASP.NET MVC which is why I came upon this thread, so I’m not sure if you you’re still checking for improvements.

I don’t like the idea of adding the new property to a partial class on the entity framework because it doesn’t allow for as much change.
Try labeling your Deparment DropDown “Department.Id” like this

<p>
    <label for="Department.Id">Department:</label>
<%= Html.DropDownList("Department.Id", new SelectList((IEnumerable)ViewData["Departments"], "Id", "Name"))%>
</p>

The ModelBinding of the MVC Framework will pick up the value and apply it to the “Id” Property of the “Department” Navigation Property. What I found is that the other values of Department are null, but that is not significant. Now you have a way of retrieving the correct Department Entity and applying it to the Department Navigation Property of the new Person Entity created in the Model Bind to your Action parameter, something like:

newPerson.Department = ctx.Department.First(d => d.DepartmentId == newPerson.Department.Id);

In doing it this way, you don’t need to update your Entity at all for a property it should have.

Leave a Comment