我真的堅持在這:我有兩個從數據庫中填充的列表框。我想將項目從一個列表複製到另一個列表。然後,這些更改必須保存在數據庫中。ASP.NET MVC:HOWTO:編輯multiselectlist(列表框)後更新數據庫
這是我的本錢:
自定義視圖模型:
public class StudentModel
{
public IEnumerable<SelectListItem> NormalStudentsList { get; set; }
public IEnumerable<SelectListItem> StudentsNoClassList { get; set; }
public string[] NormalSelected { get; set; }
public string[] NoClassSelected { get; set; }
public string Save { get; set; }
}
控制器:
public ActionResult IndexStudents(Docent docent, int id, int klasgroepid)
{
var studentModel = new StudentModel
{
NormalStudentsList = docent.GeefStudentenNormaalList(id, klasgroepid),
StudentsNoClassList = docent.GeefStudentenNoClassList(id, klasgroepid)
};
return View(studentModel);
}
[HttpPost, Authorize]
public ActionResult IndexStudentsResult(StudentModel model, string add, string remove)
{
ModelState.Clear();
(if! string.IsNullOrEmpty(add))
//update database
SaveState(model);
return View(model);
}
但我怎麼能更新數據庫?使用UpdateModel()? 還是應該使用FormCollection?但是我需要一個formCollection來處理UpdateModel()... Students表中有一個名爲「ClassID」的字段,並且在將行從1列表複製到另一個時,ID必須從當前ClassID更改爲「0 」。
我該怎麼做?我真的被困在這個......希望你能幫上忙。
這是我的看法
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ProjectenII.Models.Domain.StudentModel>"%>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
IndexStudents
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>IndexStudents</h2>
<%using (Html.BeginForm()) { %>
<%=Html.ListBoxFor(model => model.NormalSelected, new MultiSelectList(Model.NormalStudentsList, "StudentNummer", "Naam", Model.NormalSelected), new { size = "6" }); %>
<input type="submit" name="add"
id="add" value=">>" /><br />
<%=Html.ListBoxFor(model => model.NoClassSelected, new MultiSelectList(Model.StudentsNoClassList, "StudentNummer", "Naam", Model.NoClassSelected)); %>
<% } %>
<%=Html.HiddenFor(model => model.Save) %>
<input type="submit" name="apply" id="apply" value="Save!" />
</asp:Content>
您目前沒有任何關於如何訪問數據庫的信息。你是否使用實體框架,NHibernate,直接ADO.Net,別的?當這種東西被排除在外時,很難幫助將信息保存到數據庫中。 –