0

I'm wondering what's the general approach of passing a list of lookup values to a view in MVC. Currently I have 2 db tables and I'm using db first EF6 to interface. My main table has a lookup table and I want to populate a dropdownlist of my view with all the values of the lookup so that the user can pick when creating and editing.

Employee Table

id primary key
name varchar
department id - this is the id of the department in the lookup

Department table

id primary key
name varchar

Is it best to create a partial class for the employee model and add a new property called allDepartments and then in my controller call a method that gets all the departments before passing the model to the view, or is it better to dump the departments in the viewbag/viewdata dictionary?

What is the general approach here?

halfer
  • 19,824
  • 17
  • 99
  • 186
Richard Banks
  • 2,946
  • 5
  • 34
  • 71
  • You don't create a 'partial' class. You create a view model - [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc). And for a typical iplementation, refer [this question/answer](http://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) –  Jul 23 '16 at 07:41

1 Answers1

0

You need to create a ViewModel like this:

public class EmployeeViewModel
{
    public string Name { get; set; }
    [Required(ErrorMessage = "...")] // to protect against under-posting attacks
    [Display(Name = "Department")]
    public int? DepartmentId { get; set; }
    public IEnumerable<SelectListItem> Departments { get; set; }
}

Controller:

public ActionResult Create()
{
    var employeeViewModel = new EmployeeViewModel();
    employeeViewModel.Departments = GetDepartments().Select(option => new SelectListItem
    {
        Text = option.name,
        Value = option.Id.ToString()
    });
    return View(employeeViewModel);
}

// Post
public ActionResult Create(EmployeeViewModel model)
{
    // Map ViewModel to Entity and Save to db...
}

View:

@model EmployeViewModel

<div class="form-group">
    @Html.LabelFor(model => model.Name)
    @Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.Name) 
</div>

<div class="form-group">
    @Html.LabelFor(model => model.DepartmentId)
    @Html.DropDownListFor(model => model.DepartmentId, Model.Departments, "Choose...")
    @Html.ValidationMessageFor(model => model.DepartmentId)
</div>
Syed Farjad Zia Zaidi
  • 3,302
  • 4
  • 27
  • 50
  • People need to learn to point out the reason for down vote rather than just going about it... – Syed Farjad Zia Zaidi Jul 23 '16 at 17:10
  • No they do not. Your answer is OK and does not deserve a downvote, however its contains a few bad practices (and typo so it would not work). Property `Departments` should be `IEnumerable` and you build the `SelectList` in the controller, not the view (and the view becomes `@Html.DropDownListFor(m => m.DepartmentId, Model.Departments, "Choose..."). Property `DepartmentId` should be `int?` (nullable) with a `[Required]` attribute. –  Jul 24 '16 at 07:11
  • And setting the `Selected` property of `SelectListItem` is pointless - its ignored by the `DropDownListFor()` method (internally the method builds a new `SelectList` and sets the `Selected` property based on the value of the property your binding to. If you correct those issue, I will be happy to give you an upvote. –  Jul 24 '16 at 07:11