0

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>
4
  • What type is model.Id? Commented Nov 20, 2014 at 14:32
  • entity @model So.Entities.FBApprovalFilter Commented Nov 20, 2014 at 14:32
  • Sorry, this did not make a lot of sense to me. Let's be more precise - what type if property Id? Is it a list of some primitives, e.g. int or string? Or just an int? For multiple selection you need a list to receive all selected values Commented Nov 20, 2014 at 14:34
  • Well, suppose user selects multiple values. Hence multiple ints are posted. How is single int field going to accommodate them all? Commented Nov 20, 2014 at 14:41

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

it is necessary to take ViewModel ?
Yours should be ok, just the model.Id needs to be a int[] instead of just int
i understood what you explained. i have changed below code, still on submit i am getting null values @Html.ListBoxFor(model => model.Ids, new SelectList(TempData["Cities"] as MultiSelectList, "Value","Text",new { style = "width:250px" })))
Would it be possible to create a model class for your page? That's the only different I notice between our pages

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.