Hi
We just doing a project but somehow we stuck on getting Multi-select value to save into Database.
We would like to save multiple schools & sports into Db.
Is it ok to have two multi-select on the same view?
We are using Database first approach.
We been stuck here for while. Any help would be really appreciated
Here is the code:
Model:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CountiesManukau.Models
{
[MetadataType(typeof(SchoolUserMetaData))]
public partial class School
{
}
public class SchoolUserMetaData
{
public int SchoolId { get; set; }
public Nullable<int> WardId { get; set; }
public Nullable<bool> IsSelected { get; set; }
public virtual UserProfile UserProfile { get; set; }
public virtual ICollection<timesheet> Timesheets { get; set; }
public virtual Ward Ward { get; set; }
}
}
namespace CountiesManukau.Models
{
[MetadataType(typeof(SportMetaData))]
public partial class Sport
{
}
public class SportMetaData
{
public int SportId { get; set; }
[Required]
[Display(Name= "Name of Sport")]
public string Name { get; set; }
}
}
namespace CountiesManukau.Models
{
[MetadataType(typeof (TimesheetMetaData))]
public partial class SchoolSportUser
{
}
public class TimesheetMetaData
{
public int Id { get; set; }
public int UserId { get; set; }
public int SchoolId { get; set; }
public int SportId { get; set; }
public string Length { get; set; }
public bool IsSelected { get; set; }
public virtual Sport Sport { get; set; }
public virtual Term Term { get; set; }
public virtual UserProfile UserProfile { get; set; }
public virtual School School { get; set; }
public virtual Session Session { get; set; }
public IEnumerable<SelectListItem> Schools { get; set; }
public IEnumerable<string> SelectedSchools { get; set; }
Here is the controller
[HttpGet]
public ActionResult Create()
{
List<SelectListItem> listSelectListItem = new List<SelectListItem>();
foreach (School school in db.Schools)
{
SelectListItem selectListItem = new SelectListItem()
{
Text = school.Name,
Value = school.SchoolId.ToString(),
Selected = school.IsSelected.HasValue
};
listSelectListItem.Add(selectListItem);
}
TimesheetMetaData skl = new TimesheetMetaData();
skl.Schools = listSelectListItem;
ViewBag.SchoolId = new SelectList(db.Schools, "SchoolId", "Name");
ViewBag.SessionId = new SelectList(db.Sessions, "SessionId", "SelectSession");
ViewBag.SportId = new SelectList(db.Sports, "SportId", "Name");
ViewBag.TermId = new SelectList(db.Terms, "TermId", "TermName");
ViewBag.UserId = new SelectList(db.UserProfiles, "UserId", "UserName");
return View(skl);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Timesheet skl)
{
if (ModelState.IsValid)
{
UserProfile user = db.UserProfiles.Single(u => u.UserName == User.Identity.Name);
user.Timesheets.Add(skl);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.SchoolId = new SelectList(db.Schools, "SchoolId", "Name");
ViewBag.SessionId = new SelectList(db.Sessions, "SessionId", "SelectSession");
ViewBag.SportId = new SelectList(db.Sports, "SportId", "Name");
ViewBag.TermId = new SelectList(db.Terms, "TermId", "TermName");
ViewBag.UserId = new SelectList(db.UserProfiles, "UserId", "UserName");
return View(skl);
}
View
@model CountiesManukau.Models.TimesheetMetaData
<tr>
<th>
@Html.LabelFor(model => model.SchoolId, "School")
</th>
<th>
@Html.HiddenFor(model => model.SchoolId)
@Html.ListBoxFor(x=>x.SelectedSchools, Model.Schools)
@Html.ValidationMessageFor(model => model.SchoolId)
</th>
</tr>
Any help would be really appreciated we been stuck on this for while.
Thanks in Advance
Amrin