MVC4 Multiselect下拉列表保存到数据库 [英] MVC4 Multiselect dropdown list save to database
本文介绍了MVC4 Multiselect下拉列表保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我们只是在做一个项目,但不知怎的,我们坚持获取多选值以保存到数据库中。
我们会喜欢拯救多所学校和运动到Db。
在同一个视图上有两个多选项可以吗?
我们正在使用数据库第一种方法。
我们一直困在这里。任何帮助将非常感谢
这是代码:
型号:
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel.DataAnnotations;
使用 System.ComponentModel;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Mvc;
namespace CountiesManukau.Models
{
[MetadataType( typeof (SchoolUserMetaData))]
public 部分 类学校
{
}
public class SchoolUserMetaData
{
public int SchoolId { get ; set ; }
public Nullable< int> WardId {获取; set ; }
public Nullable< bool> IsSelected { get ; set ; }
public virtual UserProfile UserProfile { get ; set ; }
public virtual ICollection< timesheet>时间表{获取; set ; }
public virtual Ward Ward { get < /跨度>; set ; }
// public IEnumerable< SelectListItem>学校{得到;组; }
// public IEnumerable< string>选定的学校{get;组; }
}
}
namespace CountiesManukau.Models
{
[MetadataType( typeof (SportMetaData))]
public partial class Sport
{
}
public class SportMetaData
{
public int SportId { get ; set ; }
[必需]
[显示(名称= 体育名称 )]
public string 名称{获得跨度>; set ; }
}
}
命名空间 CountiesManukau.Models
{
[元数据类型( typeof (TimesheetMetaData))]
public partial class SchoolSportUser
{
}
public class TimesheetMetaData
{
// [Key]
// [HiddenInput(DisplayValue = false)]
public int Id { get ; set ; }
public int UserId {获得跨度>; set ; }
public int SchoolId {获得跨度>; set ; }
public int SportId {获得跨度>; set ; }
public string 长度{获得跨度>; set ; }
public bool IsSelected {获得跨度>; set ; }
public virtual Sport Sport {获得跨度>; set ; }
public virtual 术语{{span class =code-keyword> get < /跨度>; set ; }
public virtual UserProfile UserProfile { get < /跨度>; set ; }
public virtual School School { get < /跨度>; set ; }
public virtual 会话会话{ get < /跨度>; set ; }
// public List< SelectListItem>学校{得到;组; }
public IEnumerable< SelectListItem>学校{获取; set ; }
public IEnumerable< string> SelectedSchools { get ; set ; }
// public IEnumerable< SelectListItem>运动{得到;组; }
// public IEnumerable< string> SelectedSports {get;组; }
这是控制器
// GET:/ TimeSheet / Create
[HttpGet]
public ActionResult Create()
{
List< SelectListItem> listSelectListItem = new List< SelectListItem>();
foreach (学校在 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, 名称);
ViewBag.SessionId = new SelectList(db.Sessions, SessionId, SelectSession);
ViewBag.SportId = new SelectList(db.Sports, SportId, 名称);
ViewBag.TermId = new SelectList(db.Terms, TermId, TermName);
ViewBag.UserId = new SelectList(db.UserProfiles, UserId, UserName);
return 查看(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, 名称);
ViewBag.SessionId = new SelectList(db.Sessions, SessionId, SelectSession);
ViewBag.SportId = new SelectList(db.Sports, SportId, 名称);
ViewBag.TermId = new SelectList(db.Terms, TermId, TermName);
ViewBag.UserId = new SelectList(db.UserProfiles, UserId, UserName);
return 查看(skl);
}
查看
@model CountiesManukau.Models.TimesheetMetaData
< tr>
< th>
@ Html.LabelFor(model = > model.SchoolId, School)
< / th < span class =code-keyword>>
< th>
@ Html.HiddenFor(model = > model.SchoolId)
@ Html.ListBoxFor(x => x.SelectedSchools,Model。学校)
@ Html.ValidationMessageFor(model = > model.SchoolId)
< / >
< ; / tr >
任何帮助都会非常感激我们一直坚持这一点。
预付款
Amrin
解决方案
在视图中,您可以拥有任意类型的控件。要插入任意值列表,您可以使用EF(这使得它很容易)或编写一个proc,它将任意值列表作为XML并插入它们。每个值执行一次SQL调用是浪费且效率低下的。
ddf:
l ;;;;;;小>
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; }
//public IEnumerable<SelectListItem> Schools { get; set; }
//public IEnumerable<string> SelectedSchools { 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
{
//[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
// 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
解决方案
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.
ddf: l;;;;;;
这篇关于MVC4 Multiselect下拉列表保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文