0

I'm new to MVC and are having a hard time figuring some "basic" things out.
I have a ViewModel shaped as follows:

public class ProjectViewModel
{
    public int Id { get; set; }

    [Required]
    public string Title { get; set; }

    [Required]
    public string Description { get; set; }

    public DateTime CreatedDate { get; set; }

    [Required]
    [Display(Name = "Final due date")]
    public DateTime FinalDueDate { get; set; }

    [Required]
    [Display(Name = "Attached equipment")]
    public Equipment AttachedEquipment { get; set; }
}

In my Create view I would like to be able to select the value for AttachedEquipment from a dropdownlist. I have a table in my database with all the available Equipments.
I know there is an @Html helper @Html.DropDownListFor which serves this very purpose. However I fail to see how I get the values from the database and spit them out into my view.

My ProjectController looks like this:

private AdventureWorks db = new AdventureWorks();

// GET: Project
public ActionResult Index()
{
    return View();
}

[HttpGet]
public ActionResult Create()
{
    // I'm guessing this is where I need to do some magic
    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ProjectViewModel model)
{
    if (ModelState.IsValid)
    {
        var project = new Project
        {
            Title =  model.Title,
            Description = model.Description,
            CreatedDate = DateTime.Now,
            FinalDueDate = model.FinalDueDate,
            Equipment = model.Equipment
        };

        db.Projects.Add(project);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(model);
}

How do I load the Equipment values from my DB into a dropdownlist in my Create view?

tereško
  • 58,060
  • 25
  • 98
  • 150
  • possible duplicate of [MVC3 DropDownListFor - a simple example?](http://stackoverflow.com/questions/7142961/mvc3-dropdownlistfor-a-simple-example) – Mark Shevchenko Jan 15 '15 at 14:08
  • @MarkShevchenko Ok, I get the example given. But does that then mean I will always have to find the DB entry matching the selected value? –  Jan 15 '15 at 14:13
  • 1
    Yes, you will. Using EF it's quiet simple, but still tedious. Usually this logic are located in helper classes and methods. In simple app you can do it in the model directly. In complex app it's better to push this logic in a separate tier (see [Model-View-Presenter pattern](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter)). – Mark Shevchenko Jan 15 '15 at 14:21

1 Answers1

0

Since you cant bind a dropdownlist to the complex object AttachedEquipment, I would change the view model to include properties for the selected equipment and a SelectList for the options

public class ProjectViewModel
{
  public int Id { get; set; }
  [Required]
  public string Title { get; set; }
  [Required]
  public string Description { get; set; }
  public DateTime CreatedDate { get; set; }
  [Required]
  [Display(Name = "Final due date")]
  public DateTime FinalDueDate { get; set; }
  [Required(ErrorMessage="Please select equipment)]
  public int? SelectedEquipment { get; set; }
  public SelectList EquipmentList { get; set; }
}

Controller

public ActionResult Create()
{
  ProjectViewModel model = new ProjectViewModel();
  // Assumes your Equipments class has properties ID and Name
  model.EquipmentList = new SelectList(db.Equipments, "ID", "Name");
  return View(model);
}

View

@model ProjectViewModel
@using(Html.BeginForm())
{
  ....
  @Html.DropDownListFor(m => m.SelectedEquipment, Model.EquipmentList, "--Please select--")
  ....
}

Alternatively you can bind to m => m.AttachedEquipment.ID (assuming Equipment contains property ID)