1

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!

bbusdriver
  • 1,577
  • 3
  • 26
  • 58
  • A ` –  Jun 13 '16 at 07:59
  • @StephenMuecke Thank you for your reply, it helped but I can see data is being passed, and it is not going into skills column. After using your suggestion, column name for skills is gone in my sql employee table. How do I save it into the table? – bbusdriver Jun 14 '16 at 07:56
  • You need to use a view model in the view that has `IEnumerable Skills` (and `string Name`) and it should also contain a property `IEnumerable SkillsList` rather than using `ViewBag` (you have not shown your model for `Skill` or how your generating the `SelectList` so it may need to be `IEnumerable Skills` if your binding to an `int SkillId` property of `Skill`) –  Jun 14 '16 at 08:01
  • Can I add more view models in the view? thought it had restriction. currently I am using @model test.Models.Employee in my create view. Not sure what happens if I remove the current one and replace with what you mentioned. – bbusdriver Jun 14 '16 at 08:11
  • Just create a `class EmployeeVM` view model with the properties you need and replace `@model Employee` with `@model EmployeeVM`. You should always be using a view model, especially when editing data. Refer [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Jun 14 '16 at 08:15
  • Thanks Stephen, what you mentioned sounds more like my second approach I mentioned in my post. my model for skills would look very simple with its int skillId and string skills. But I mean saving an array of data in one column is not possible? For example, multiselectList that has been passed (java, c#) cannot be db.SaveChanges() properly in employee table? – bbusdriver Jun 14 '16 at 08:21
  • You need to read the link I gave you. In the POST method you map your view model to an instance of the data model. –  Jun 14 '16 at 08:30

1 Answers1

1

I used chosen to create a filter based on multiselects, and the data model uses arrays, so in your case it would be

public string[] Skills { get; set; }
devio
  • 36,858
  • 7
  • 80
  • 143
  • Thank you very much, but now I have a question as to saving an array of strings into my Employee table. I can see data is being passed, but its data is not going into skills column. I mean, after using 'public string[] Skills { get; set; }' column name for skills is gone in my sql employee table. – bbusdriver Jun 14 '16 at 07:47
  • don't you have separate classes for data access and user interface? – devio Jun 14 '16 at 07:56
  • I don't think so I thought ModelState.IsValid = true would save data in my sql table – bbusdriver Jun 14 '16 at 08:07