问题描述
目前我有两个列表框从我的观点一个数据库绑定的数据...我已经使用了自定义视图模型,它不是强类型。
这里的code:
<%@页标题=LANGUAGE =C#的MasterPageFile =〜/查看/共享/的Site.Master继承=System.Web.Mvc.ViewPage< ProjectenII.Models.Domain.StudentModel>中%GT;< ASP:内容ID =内容1ContentPlaceHolderID =TitleContent=服务器>
IndexStudents
< / ASP:内容>< ASP:内容ID =内容2ContentPlaceHolderID =日程地址搜索Maincontent=服务器>
< H2> IndexStudents< / H> < DIV CLASS =主编场>
<%:Html.ListBox(IndexStudentsNormal,Model.NormalStudentsList)%GT;
< / DIV> <输入类型=提交名称=加上
ID =添加VALUE =>>中/>
< BR />
<输入类型=提交名称=删除
ID =删除VALUE =<< /> < DIV CLASS =主编场>
<%:Html.ListBox(IndexStudentsNoClass,Model.StudentsNoClassList)%GT;
< / DIV> <输入类型=提交名称=应用ID =应用价值=保存! />
< / ASP:内容>
好了,现在我想移动使用两个按钮(>>)和这两个列表框之间的项目(其中p<),当用户点击应用按钮,更改必须记录在数据库中。
StudentModel:
命名空间ProjectenII.Models.Domain
{
公共类StudentModel
{
公共IEnumerable的< SelectListItem> NormalStudentsList {搞定;组; }
公共IEnumerable的< SelectListItem> StudentsNoClassList {搞定;组; }
}
}
控制器:
公众的ActionResult IndexStudents(导赏讲解员,INT ID,INT klasgroepid)
{
VAR studentModel =新StudentModel
{
NormalStudentsList = docent.GeefStudentenNormaalList(ID,klasgroepid)
StudentsNoClassList = docent.GeefStudentenNoClassList(ID,klasgroepid)
}; 返回查看(studentModel);
}
所以,我怎么能得到一个列表框的设定值,并将其保存到另一个?
事后,这些变化必须在数据库中记录下来。
因此,更新数据库。
我可以利用的ModelState在更新数据库中的值?
我不是在asp.net mvc的非常好,所以我希望你明白我的意思...
在此先感谢!
有已的有关使用jQuery的双面多选择列表(它两箱移动箱之间的物品的东西)的一篇文章。它不再存在,但它的本质是以下(根据我的的例子,这是不是一块支持软件)...
它描述模型,包括获得所选的值,控制器和深入浅出的视图。
您需要对可能接受选定的数值模型,该模型将被送回作为一个字符串,而不是作为一个SelectListItem(属性即从被选择的选项的值:&LT ;期权价值=student1选择>先生学生的One< /选项>
公共类StudentModel
{
公共IEnumerable的< SelectListItem> NormalStudentsList {搞定;组; }
公共IEnumerable的< SelectListItem> StudentsNoClassList {搞定;组; }
公共IEnumerable的<串GT; StudentsSelected {搞定;组; }
}
和则会使列表框适用于这个属性。
<%= Html.ListBox(StudentsSelected,...
Currently I have 2 listboxes binding data from a database on my view... I've used a custom Viewmodel and it is not strongly-typed.Here's the code:
<%@ 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>
Well, now I want to move items between those two listboxes using two buttons (>>) and (<<) And when the user clicks the apply button, the changes must be recorded in the database.
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);
}
So how can I get the selected value of one listbox and save it to the other one?And afterwards, the changes must be written down in the database.So UPDATE the database.Can I make use of modelstate to update the values in the database?
I'm not very good at asp.net mvc, so I hope you understood me...
Thanks in advance!!
There was an article about using the jQuery Two Sided Multi Select List (which does the two-box moving items between boxes stuff). It no longer exists, but the essence of it is below (based on my two sided multi-select plugin example, which is not a piece of supported software)...
It describes the Model, including obtaining the selected values, the Controller and the View in simple terms.
You would need a property on the model that could accept the selected values, which would be sent back as a string, rather than as a SelectListItem (i.e. the value from the options that are selected: <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; }
}
And then make the ListBox apply to this property
<%= Html.ListBox("StudentsSelected", ...
这篇关于ASP.NET MVC:两个列表框之间移动项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!