Pregunta

I have a many-to-many relationship in NHibernate, and I'm trying to bind all items in a Html.ListBoxFor() to my Model, but that list always comes with 0 items in the Model passed to Controller.

Here's what I've got so far:

Role.hbm.xml > 'left' side of many-to-many relationship

<bag name="LdapGroups" table="CADU_TB_ROLE_LDAP_OBJECTS" cascade="all">
    <key column="ID_ROLE" />
    <many-to-many  column="ID_LDAP_OBJECT" class="LdapObject" />
</bag>

Role.cs > Model

public class Role {
    public virtual IList<LdapObjects> LdapGroups {
        get;
        set;
    }
}

RoleController.cs > Controller

public class RoleController: Controller {
    public ActionResult Create(Role obj) {
        // Create logic here
        // here, obj.LdapGroups.Count is equal to 0, but instantiated
    }
}

Create.aspx > View (right list in image below)

<%= Html.ListBoxFor(model => model.LdapGroups,
    new List<CaduMVC.Models.LdapObject>()
        .Select(o => new SelectListItem() {
            Value = o.Id.ToString(),
            Text = o.Description
        }),
        new { id="lbSelectedGroups",
              @class = "form-control",
              style = "height: 120px;" }) %>

Here's the View (Items are passed from left to right using JQuery. Item type is LdapObject)

I need to get all items from lbSelectedGroups (right list) and put them in the obj (obj.LdapGroups) param (Create Action), but it comes with 0 items

How can I do that?

¿Fue útil?

Solución 2

After some debugging, I found that Html.ListBoxFor() method sends a collection of values ​​(value attribute from option tag), and not the ListItem objects (which I was expecting). I created a transient property in the Model object of type int[], which receives that list of values, then create and insert the deattached objects into the list of LdapObjects. As follows:

Role.cs > Model

public class Role {
    public virtual IList<LdapObjects> LdapGroups {
        get;
        set;
    }
    // Transient
    public virtual int[] LdapGroupsSelected {
        get;
        set;
    }
}

Create.aspx > View (right list in image above)

<%= Html.ListBoxFor(model => model.GruposLdapSelecionados,
    new Iesi.Collections.Generic.HashedSet<CaduMVC.Models
            .LdapObject>()
        .Select(o => new SelectListItem() {
            Value = o.Id.ToString(),
            Text = o.Descricao,
        }),
        new { id="lbGruposSelecionados",
            @class = "form-control",
            style = "height: 120px;" }) %>

RoleController.cs > Controller

public ActionResult Create(Role obj) {
    . . .
    for(int i = 0; i < obj.LdapGroupsSelected.Length; i++) {
        obj.LdapGroups.Add(new LdapObject(Convert.ToInt32(
                obj.LdapGroupsSelected[i])));
    }
    session.Merge(obj);
}

Otros consejos

At a first glance what i see is there is controller action missing when click on >> button. That action should get all items in the both lists via jquery get using a pattern and pass a collection of items to controller. The action will push items to second box and send the result. The jquery success method take data from collections and refill both List Boxes.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top