Click here to Skip to main content
15,867,860 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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:
C#
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; }

        //public IEnumerable<SelectListItem> Schools { get; set; }
        //public IEnumerable<string> SelectedSchools { get; set; }
    }
   
}

C#
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; }
    }

}


C#
namespace CountiesManukau.Models
{
    [MetadataType(typeof (TimesheetMetaData))]
    public partial class SchoolSportUser
    {
    }
    public class TimesheetMetaData
    {
        //[Key]
        //[HiddenInput(DisplayValue = false)]
        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 List<SelectListItem> Schools { get; set; }
    public IEnumerable<SelectListItem> Schools { get; set; }
    public IEnumerable<string> SelectedSchools { get; set; }

  //public IEnumerable<SelectListItem> Sports { get; set; }
  //public IEnumerable<string> SelectedSports { get; set; }




Here is the controller
C#
// GET: /TimeSheet/Create
       [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);
        }

        //
        // POST: /TimeSheet/Create

        [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
Posted
Updated 22-Dec-13 12:58pm
v2

You can have as many controls of any type you like, in a view. To insert an arbitrary list of values, you would either use EF ( which makes it easy ) or write a proc that takes an arbitrary list of values as XML and inserts them. Doing one SQL call per value is wasteful and inefficient.
 
Share this answer
 
C#
ddf:
l;;;;;;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900