Q:

DropDownlistFor Creating New Entity with MVC3

I'm developing a small app in order to better understand how MVC3 anda Razor works. I'm using MVC3, all code was generated automatically (dbContext via T4, Controller via Add Controller, Databese from EDMX model...).
In my model I have this simple model:
http://i.stack.imgur.com/nyqu4.png
public partial class Application
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ApplicationType ApplicationType { get; set; }
}

public partial class ApplicationType
{
    public int Id { get; set; }
    public string Type { get; set; }
}

As you can see, ApplicationType is basically an enum (shame that EF 4 has no support for enums). So, in my ApplicationController I have this:
public ActionResult Create()
{
    ViewBag.AppTypes = new SelectList(db.ApplicationTypes.OrderBy(c => c.Type), "Id", "Type");
    return View();
} 

[HttpPost]
public ActionResult Create(Application application)
{
    if (ModelState.IsValid)
    {
        db.Applications.Add(application);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(application);
}

And in my view:
@Html.DropDownListFor(model => model.ApplicationType.Id, (SelectList)ViewBag.AppTypes, "Choose...")

Now I'm facing two problems:
1) ApplicationType not being populated:
As @Html.DropDownListFor renders only a simple select, it fills the ID, but does not fill Type property as you can see below (sorry, I can't post images as I'm new here):
http://i.stack.imgur.com/96IR1.png
In the picture you can see that the ID is ok, but Type is empty.
What I'm doing wrong?
2) Duplicated Data
The second problem is that if I fill the Type property manually during debug (simulating a correct workflow scenario), ApplicationType is being duplicated in the database, instead of only referring to an old registry.
So, how can I make @Html.DropDownListFor refer to a previous existing item instead of creating a new one?
Thanks for your help!

A:

I believe the mistake you're making is using your domain models in the view and assuming that on post the entire model should be completely binded and ready to store in the database. While it is possible to use domain models in the view, it's better practice to create separate View Models.
For example :
public class ApplicationViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }

    public SelectList ApplicationTypeList { get; set; }
    public string ApplicationTypeId { get; set; }
}

In your view:
@Html.DropDownListFor(model => model.ApplicationTypeId, Model.ApplicationTypeList , "Choose...")

In your controller
[HttpPost]
public ActionResult Create(ApplicationViewModel model)
{
    if (ModelState.IsValid)
    {
        Application application = new Application()
        {
           Id = model.Id,
           Name = model.Name,
           ApplicationType = db.ApplicationTypes
              .First(a => a.Id == model.ApplicationTypeId);

        };
        db.Applications.Add(application);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(model);
 }

You can then make verifying that your View Model's ApplicationTypeId corresponds to a real application type part of your  modelstate's verification. You can use AutoMapper to speed up the process of converting view models to domain models.

