2

I'm really stuck at this: I have two listboxes populated from a database. I want to copy items from one list to the other. Then the changes must be saved in the database.

This is what I've got:

Custom ViewModel:

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; }
    }

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);
        }

        [HttpPost, Authorize]
        public ActionResult IndexStudentsResult(StudentModel model, string add, string remove)
        {
            ModelState.Clear();
           (if! string.IsNullOrEmpty(add))
               //update database
            SaveState(model);
            return View(model);
        }

But how can I update the database?? Using UpdateModel()? or should I work with FormCollection? But I need a formCollection to work with UpdateModel()... The Students table has a field named "ClassID", and when copying the rows from 1 list to the other, the ID has to change from the current ClassID to "0".

How can I do that? I'm really stuck at this... hope you can help.

This is my View

<%@ 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>
Lorenzo
  • 193
  • 5
  • 14
  • You don't have any information as to how you're accessing the database currently. Are you using Entity Framework, NHibernate, straight up ADO.Net, something else? It's hard to help saving info to the database when this kind of stuff is left out of the question. – Chris Conway Aug 19 '11 at 17:24

2 Answers2

2

Your problem is related to returning a List from the view... check this post by Phil Haack:

Model Binding To A List

Here you can see I ran into a similar problem. In my case a used checkboxes to select items in a list. The solution proposed guided me in the right direction but it wasn't the one I used, I used Phil's post.

My Post

Hope this helps.

Community
  • 1
  • 1
AJC
  • 1,853
  • 1
  • 17
  • 28
  • 1
    @laurent jejeje... I wrongly assumed you were using EF... Chris is right, you don't provide any info as to what Technology you use for Data Access. Please provide such so as to allow us to recommend a course of action. – AJC Aug 19 '11 at 18:16
0

We may also achieve using Editor helper, but making all of the multiselectlist elements selected before submit will work:

$("#NormalSelected option").prop("selected", true);

This will pass multiselectlist items to controller.