Hi I am still new to MVC4, I would like to use HTML helper to create a select box that would allow users to select multiple items using CTRL Click and store results using EF. Here is my view
@model AtomicQuestionnaire.Models.Questionnaire
@{
ViewBag.Title = "Create";
}
@{
var ListInterest = new List<SelectListItem>()
{
new SelectListItem(){Value="Stamp Collecting",Text="Stamp Collecting"},
new SelectListItem(){Value="Listening to Music",Text="Listening to Music"},
new SelectListItem(){Value="Reading",Text="Reading"}
};
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Interest)
</div>
<div class="editor-field">
@Html.ListBoxFor(model => model.Interest, ListInterest, new { size = "15", Multiple = "multiple" })
@Html.ValidationMessageFor(model => model.Interest)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
And here is my model
public class Questionnaire
{
public int ID { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string VisitorDesc { get; set; }
public string Interest { get; set; }
public int SiteRating { get; set; }
}
and my controller for create action
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Questionnaire questionnaire)
{
if (ModelState.IsValid)
{
db.Questionnaires.Add(questionnaire);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(questionnaire);
}
I was only able to store the first item of the html selection box to database. Would you be able to tell me what I did wrong? I would like to store all selected items into DB. Thanks