MVC4 Get listboxfor Text & Value properties

1.2k views Asked by At

In My MVC4 EF5 App i'm using 2 listboxes. each listbox is bounded to a List property of my view model.

i would like to get all listboxes items (and i mean all SelectedListItem - Selected, Text, Value) when i submit a from.

This is how i do so (with no sucess..)

My View Model:

public class PriorityViewModel
{

    public List<SelectListItem> lst_fromCEID { get; set; }
    public List<SelectListItem> lst_C2C { get; set; }

    public PriorityViewModel()
    {
        lst_C2C = new List<SelectListItem>();
        lst_fromCEID = new List<SelectListItem>();
    }
}

My Action:

public ActionResult Index()
    {
        var model = new PriorityViewModel();

        var c2c_ceid_source = _unitOfWork.ActionRepository.GetC2C_SourceCEID();
        model.lst_fromCEID = _unitOfWork.EntityRepository.GetEntitiesByC2CSource(c2c_ceid_source);

        return View(model);
    }

    public ActionResult UpdatePriority(PriorityViewModel model)
    {
        //save manual changes to DB
        SaveManualPriorityToDB(model);
        ...
    }

My View:

@model TRM.Models.ViewModel.PriorityViewModel

@{
    ViewBag.Title = "Index";
}


@*@Html.Action("Run");*@


@using (Html.BeginForm("UpdatePriority", "Priority", FormMethod.Post))
{
    @Html.Hidden("demoQuantity", Model.demoQuantity)
    @Html.Hidden("installQuantity", Model.installQuantity)
    @Html.Hidden("CIP_Quantity", Model.CIP_Quantity)
    @Html.Hidden("C2C_Quantity", Model.C2C_Quantity)
    <table>

        <tr>
            <td>
                From CEID
                @Html.ListBoxFor(model => model.lst_fromCEID, @Model.lst_fromCEID, new { width = "50px" })
            </td>

            <td style="width:50px">
                <input id="btnRemoveAllC2C" type="button" value=" << " onclick="removeallItemsC2C();" />
                <input id="btnAddAllC2C" type="button" value=" >> " onclick="addallItemsC2C();" />
                <input id="btnAddC2C" type="button" value="  > " onclick="addItemC2C();" />
                <input id="btnRemoveC2C" type="button" value="  < "  onclick="removeItemC2C();" />
            </td>

            <td style="width:50px">
                C2C
                @Html.ListBoxFor(model => model.lst_C2C, new MultiSelectList(Model.lst_C2C), new { width = "50px" })
            </td>
        </tr>

        <tr>
            <td>
                <h2>Install & Orphan</h2>
            </td>
        </tr>


    </table>

    <input type="submit" value="Run Transition!" onclick="SelectAllItems()"/>
}

@section scripts {
    <script src="~/Scripts/MyScripts/PriorityScript.js"></script>
}

My Js File:

function addItemC2C() {
    $("#lst_fromCEID option:selected").appendTo("#lst_C2C");
    $("#lst_C2C option").attr("selected", false);
}

function addallItemsC2C() {
    $("#lst_fromCEID option").appendTo("#lst_C2C");
    $("#lst_C2C option").attr("selected", false);
}

function removeItemC2C() {
    $("#lst_C2C option:selected").appendTo("#lst_fromCEID");
    $("#lst_fromCEID option").attr("selected", false);
}

function removeallItemsC2C() {
    $("#lst_C2C option").appendTo("#lst_fromCEID");
    $("#lst_fromCEID option").attr("selected", false);
}

if i set the viewModel properties to List i can get the listbox text out on button submit. but this is not enough for me, i need Text and Value.

Thx..

0

There are 0 answers