ASP.NET mvc: moving items between two lists

Currently, I have 2 lists that bind data from a database on my view ... I used a custom Viewmodel model and not very typed. Here is 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 these two lists using the two buttons (β†’) 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 list and save it in another? And after this change must be recorded in the database. Therefore, UPDATE the database. Can I use modelstate to update values ​​in a database?

I am not very good at asp.net mvc, so I hope you understand me ...

Thanks in advance!

+2
asp.net-mvc move listbox
source share
2 answers

An article was published about using JQuery Two Sided Multi Select List (which moves two boxes between box items). It no longer exists, but its essence is lower (based on my two-way multi-channel plugin , which is not part of the supported software) ...

It describes the Model, including retrieving selected values, Controller, and View in simple terms.

You will need a model property that can take the selected values ​​that will be sent back as a string, and not as SelectListItem (that is, the value from the selected options: <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 applicable to this property.

 <%= Html.ListBox("StudentsSelected", ... 
+1
source share

Saving it in MVC, mail does not send all the parameters, only the selected ones. 1. You can use ajax / json to report what has been done. Send this option and to which list it was moved, and then you can update the database with each move. 2. You can write all the parameters (in the view) in a hidden field string (comma), then process the function descriptor, saving 2 lists.

Here is some help with the jquery part. Moving items in double lists

0
source share

All Articles