I'm using entity framework and jquery chosen plugin to get multiselected dropdownlist values for Skills property. I've struggled to make the chosen plugin work in my view, and figured it out, but now I face another problem of passing those multiselected values (ex: Skills such as Java, c#, javascript) into controller and save into my Employee table.
public IEnumerable<string> Skills { get; set; }
Above code is currently on top of my head to save multiple values, but not sure how to properly use it. Thinking about multiple ways of doing it, but I definitely need guidance.
I have a model that looks like:
public class Employee
{
public string Name { get; set; }
public string Skills { get; set; }
}
and my controller:
public ActionResult Create([Bind(Include = "Name,Skills")] Employee employee)
{
if (ModelState.IsValid)
{
db.Employees.Add(employee);
db.SaveChanges();
return View("Success");
}
return View(employee);
}
View:
@using (Html.BeginForm())
{
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
@Html.LabelFor(model => model.Skills)
@Html.ListBoxFor(model => model.Skills, ViewBag.skillList as MultiSelectList,
new { @class = "chzn-select", id="skills", data_placeholder = "Choose Skills..." })
@Html.ValidationMessageFor(model => model.Skills)
<input type="submit" value="Create" class="btn btn-default" />
}
Another approach I am thinking is to create a Skill table that Employee table can have navigation property. An employee can have any number of skills, so the Skill navigation property is a collection. But honestly have little knowledge about this and need guidance for this too. For example:
public class Employee
{
public string Name { get; set; }
public int SkillID { get; set; }
public virtual ICollection<Skill> Skills { get; set; }
}
If any of these approaches don't make sense, I'd appreciate it if you can tell me why and how I can properly use it. Thanks!