0

Here is my Model

  public class NewsViewModel
    {
        public NewsViewModel()
        {

            this.Categories = new List<Category>();

        }
        public int NewsId { get; set; }
        [Required(ErrorMessage = "Please enter News Title")]
        public string NewsTitle { get; set; }
        public IEnumerable<Category> Categories { get; set; }


    }

I need to set Selected to be True if id exists in NewsViewModel.Categories collection

private IEnumerable<SelectListItem> GetCategories()
{

    return db.Categories .Select(s=>new SelectListItem {
        Value=s.Id.ToString(),
        Text=s.Name,
        Selected = model.Categories.Select(x => x.CategoryId).Contains(s.CategoryId);

}

And in View:

@Html.ListBoxFor(s => s.SelectedCategoriesIds, @Model.AllCategories, new { id = "DropDownList2", multiple = "multiple", @class = "form-control" })
Lucy
  • 243
  • 1
  • 4
  • 18
  • You have an open bracket without a close bracket in your second code snippet FYI. – Chris H. Jul 21 '16 at 22:11
  • Also, what's the problem with what you have now? Are you getting an error? Is it just not returning what you want? – Chris H. Jul 21 '16 at 22:12
  • @ChrisH. i get error Additional information: Unable to create a constant value of type 'Assignment.Category'. Only primitive types or enumeration types are supported in this context. – Lucy Jul 21 '16 at 22:22
  • The question is why are you wanting to set the `Selected` property of `SelectListItem`? If you are using `Html.DropDownListFor()` of `Html.ListBoxFor()` in your view (which you should be doing), then the `Selected` property is ignored - its the value of the property your binding to that determines what is selected (internally the methods build a new `IEnumerable` based on the value(s) in the property) –  Jul 22 '16 at 12:32
  • @StephenMuecke, Thank you for help :) Can you please tell me how can i use Html.ListBoxFor() in View to achieve what you are saying.. Thank you – Lucy Jul 22 '16 at 17:16
  • @StephenMuecke Here is my code ... You can use it to make it selected value true Html.ListBoxFor(s => s.SelectedCategoriesIds, Model.AllCategories, new { id = "DropDownList2", multiple = "multiple", class = "form-control" }) – Lucy Jul 22 '16 at 17:18
  • Refer [this DotNetFiddle](https://dotnetfiddle.net/gHGaTy) for an example. Note there is no need for `multiple = "multiple"`. And to prove that setting the `Selected` property has no effect, comment out the line in the controller that sets `Selected=true` for all `SelectListItem`'s and run it again - note that it makes no difference at all - its the value of `SelectedCategories` which determines what is selected. –  Jul 22 '16 at 23:51

2 Answers2

3

See this question; you cannot use Contains with non-primitive types (like Category in this case). A different way to write your query that works around this limitation would be:

return db.Categories.Select(s=>new SelectListItem {
    Value=s.Id.ToString(),
    Text=s.Name,
    Selected = model.Categories.Exists(z => z.CategoryId == s.CategoryId);
}

Instead of selecting the CategoryIds from model.Categories and then checking if s.CategoryId is in that list, we can check to see if there Exists() an item in model.Categories for which the CategoryId is the same as s.CategoryId.

Community
  • 1
  • 1
Chris H.
  • 2,544
  • 19
  • 32
0

Add the condition on the "selected" as a question mark condition.

    var listSiteId = (from site in db.GetSiteId().ToList()
                                      select new SelectListItem
                                      {
                                          Value = site.SITEID,
                                          Text = site.NAME,
                                          Selected = (dimension.DISPLAYVALUE == site.SITEID) ? true : false,
                                      }).ToList();
                    ViewBag.SiteId = listSiteId;
dfortun
  • 704
  • 6
  • 7