A: 

Improve your Edit controlling so that it handles the exceptions that are thrown and redisplays the input the user has typed in so far. I'm sure you were about to ;)

Update your view to have validators:

<label for="Name">Name:</label>
<%= Html.TextBox("Name", Model.Name) %>
<%= Html.ValidationMessage("Name", "*") %>

and then utilize them in your editing:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Person Model)
{
    try
    {
       ctx.AttachUpdated(Model);  //extension
       ctx.SaveChanges();
       return RedirectToAction("Index");
    }
    catch
    {
        foreach (var err in Model.Errors)
          ModelState.AddModelError(err.PropertyName, err.ErrorMessage)

        return View(Model);
    }
}
womp
"'....Person' does not contain a definition for 'Errors' and no extension method 'Errors' accepting a first argument of type '....Person' could be found (are you missing a using directive or an assembly reference?)"
Zack Peterson
It depends on what framework you're using. Lightspeed and Linq2Sql give you the Errors property on each Entity. If you're building your Entities manually instead of using an ORM, then you'll need to build that property into your partial class for Person.
womp
Something like Listing 3 and 4 in this article: http://www.asp.net/Learn/mvc/tutorial-16-cs.aspx
womp
+5  A: 

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.

good and clean! Zack, you could also set EntityKey on your controllers Edit method:newPerson.DepartmentReference.EntityKey = new EntityKey("YourEntities.Department","DepartmentId", int.Parse(Request.Form["DepartmentId"]));
Junior Mayhé