当前,我有2个列表框绑定视图上来自数据库的数据...我使用了自定义的Viewmodel,但它不是强类型的。这是代码:
<%@ 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>
<div class="editor-field">
<%: Html.ListBox("IndexStudentsNormal", Model.NormalStudentsList)%>
</div>
<input type="submit" name="add"
id="add" value=">>" />
<br />
<input type="submit" name="remove"
id="remove" value="<<" />
<div class="editor-field">
<%: Html.ListBox("IndexStudentsNoClass", Model.StudentsNoClassList)%>
</div>
<input type="submit" name="apply" id="apply" value="Save!" />
</asp:Content>
好,现在我想使用两个按钮(>>)和(<
StudentModel:
namespace ProjectenII.Models.Domain
{
public class StudentModel
{
public IEnumerable<SelectListItem> NormalStudentsList { get; set; }
public IEnumerable<SelectListItem> StudentsNoClassList { get; set; }
}
}
Controller:
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);
}
因此,如何获取一个列表框的选定值并将其保存到另一个列表框?然后,必须将更改记录在数据库中。因此,更新数据库。我可以使用modelstate更新数据库中的值吗?
我不太擅长asp.net mvc,所以希望您能理解我...
提前感谢!
那里是一篇有关使用jQuery双面多选列表的文章(它在两个盒子之间移动两个盒子)。它不再存在,但是其本质如下(基于我的two sided multi-select plugin示例,它不是受支持的软件)...
它描述了模型,包括简单地获得所选值,控制器和视图。
[您将在模型上需要一个属性,该属性可以接受选定的值,该属性将作为字符串而不是作为SelectListItem(即,从所选择的选项中的值:<option value="student1" selected>Mr Student One</option>
]发送回)
public class StudentModel { public IEnumerable<SelectListItem> NormalStudentsList { get; set; } public IEnumerable<SelectListItem> StudentsNoClassList { get; set; } public IEnumerable<string> StudentsSelected { get; set; } }
然后将ListBox应用于此属性
<%= Html.ListBox("StudentsSelected", ...
保存在MVC中,回发不会发送所有选项,仅发送选定的选项。1.您可以使用ajax / json来报告所做的删除。将选项发送到该列表以及将其移动到的列表,然后可以在每次移动时更新数据库。2.您可以将所有选项(提交时)写入一个隐藏的字段字符串(逗号为delim),然后使用Post函数处理这两个列表。
提出这个问题,最后这就是我为获得结果所做的事情。