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!

7
  • A <select multiple> posts back an array of values do the property in your model needs to be public IEnumerable<string> Skills { get; set; } (not string Skills) Commented Jun 13, 2016 at 7: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? Commented Jun 14, 2016 at 7:56
  • You need to use a view model in the view that has IEnumerable<string> Skills (and string Name) and it should also contain a property IEnumerable<SelectListItem> 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<int> Skills if your binding to an int SkillId property of Skill) Commented Jun 14, 2016 at 8: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. Commented Jun 14, 2016 at 8: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? Commented Jun 14, 2016 at 8:15

1 Answer 1

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; }
Sign up to request clarification or add additional context in comments.

3 Comments

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.
don't you have separate classes for data access and user interface?
I don't think so I thought ModelState.IsValid = true would save data in my sql table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.