KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm using the ASP.NET MVC and the ADO.NET Entity Framework together. I want my Views and Controllers strongly-typed. But how am I supposed to handle entity associations? Here's a simple example: A Person has one Department. A Department has zero or more People. My controller passes an instance of a Person object and a collection of all the Department objects to the View. public class PersonController : Controller { ... // // GET: /Person/Create public ActionResult Create() { Person Model = new Person(); Model.Id = Guid.NewGuid(); ViewData["Departments"] = db.Department; return View(Model); } ... } My View has a "Department" DropDownList with all departments as options. <% using (Html.BeginForm()) {%> <fieldset> <legend>Fields</legend> <p> <label for="Id">Id:</label> <%= Html.TextBox("Id") %> <%= Html.ValidationMessage("Id", "*") %> </p> <p> <label for="Name">Name:</label> <%= Html.TextBox("Name") %> <%= Html.ValidationMessage("Name", "*") %> </p> <p> <label for="Department">Family:</label> <%= Html.DropDownList("Department", new SelectList((IEnumerable)ViewData["Departments"], "Id", "Name"))%> <%= Html.ValidationMessage("Department", "*")%> </p> <p> <input type="submit" value="Create" /> </p> </fieldset> <% } %> But, the Person object posted to the controller has no Department and fails! public cl
Tags (comma-separated)
Save Edits
Cancel