Here is my code for my AddNewProductViewModel
using AccessorizeForLess.Data;
using System.Collections.Generic;
using System.ComponentModel.DataA
First I would change your ViewModel to include a SelectedCategoryId
and I would change your options to be Categories
.
Without seeing the code for your get
I am assumbing ProductCategory
is something like the following:
public class ProductCategory {
public int Id {get;set;}
public string Name { get;set;}
}
Your razor mark-up would them become:
@Html.LabelFor(model => model.Categories, new { @class = "control-label col-md-2" })
@Html.DropDownListFor(model => model.SelectedCategoryId,
new SelectList(Model.Categories, "Id", "Name"), "- Please Select -")
@Html.ValidationMessageFor(model => model.Categories)
The first parameter is the selected catgegory and your options are populated from Categories
.
WorkingFiddle