简体   繁体   中英

Selecting multiple from @Html.ListBoxFor

I have added @Html.ListBoxFor i am able to bind it properly but unable to get multiple selected items after submit. Am i missing something ?

  // Below is the code for binding

  public ActionResult Create()
    {
        var cities = So.BL.City.GetCities();

        SelectList cityList = new SelectList(cities, "Id", "Name", cityId);
        TempData["Cities"] = cityList;

        return View("Create");
    }


 [HttpPost]
    public ActionResult Create([Bind(Include="Keywords,Cities")] So.Entities.Approval filter)
    {
        if (ModelState.IsValid)
        {


        }
        return View(filter);
    }

Below is the view file code. I dont have a view model just entities

 @using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)


<table style="width: 100%">
    <tr>
        <td>
            Cities:
        </td>
    </tr>
    <tr>
       @* <td>
              @Html.DropDownList("Cities", (SelectList)TempData["Cities"])    
        </td>*@
    </tr>
    <tr>
        <td>
            @Html.ListBoxFor(model => model.Id, new SelectList(TempData["Cities"] as MultiSelectList, "Value","Text",new { style = "width:250px" })))


        </td>
    </tr>
    <tr>
        <td>
            @Html.LabelFor(model => model.Keywords)
        </td>
    </tr>
    <tr>
        <td>
            @Html.EditorFor(model => model.Keywords)

        </td>
    </tr>
    <tr>
        <td>

        </td>
    </tr>

    <tr>
        <td>
            <input type="submit" value="Create" class="button" />
        </td>
    </tr>
</table>

Assuming you have changed the ID property to typeof int[] as per the comments, you still have problems, the main one being that your POST method has

 [Bind(Include="Keywords,Cities")]

which excludes binding of the ID property so it will always be null on post back. When ever you use the [Bind] attribute you should reconsider what you doing and use a view model to display/edit just the properties you want, including a property for the SelectList .

You also have some pointless code in the @Html.ListBoxFor() method. TempData["Cities"] is already a SelectList so new SelectList(TempData["Cities"] as MultiSelectList, "Value","Text" is converting the SelectList to a MultiSelectList and then creating a new SelectList form it. All you need is

@Html.ListBoxFor(model => model.ID, (SelectList)TempData["Cities"], new { style = "width:250px" })

In addition, the 3rd parameter in this

SelectList cityList = new SelectList(cities, "Id", "Name", cityId);

is not required (not sure where you declared cityId because as it is, your code does not compile). The ListBoxFor() method selects the options based on the value of your ID property, and ignores the selectedValue parameter in the SelectList constructor.

Finally, in your POST method, if the model is not valid you return the view. You need to reassign the value of TempData["Cities"] or this will be null in the view and throw an exception.

Like Andrei is suggesting, you need to bind the selected value to an array type.

Front End:

    <div class="DualListBoxDIV">
        @Html.ListBoxFor(model => model.RelatedNewsArticlesSelected, new MultiSelectList(Model.AssignedRelatedNewsArticles, "Value", "Text", Model.RelatedNewsArticlesSelected), new { @class = "SelectedBox", Size = 10 })
    </div>

Back End:

    public string[] RelatedNewsArticlesSelected { get; set; }
    public IEnumerable<SelectListItem> AssignedRelatedNewsArticles { get; set; }
    public IEnumerable<SelectListItem> UnassignedRelatedNewsArticles { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM