0

I've got a DropdownListFor that serves to hold a list of Tags. It is important to say that I will have 3 of these identical drop downs, as you can have up to 3 tags associated per request.

I am using the ViewModel method and would prefer to stay away from ViewBag.

This is my view model. The tag list holds ALL tags. And the selected tags holds any current associated tags with the request object.

public class editRequestViewModel
{
    public Request userRequest { get; set; }
    public List<SelectListItem> Tags { get; set; }
    public List<SelectListItem> SelectedTags { get; set; }
}

This is my controller specific to the tags.

  public ActionResult Edit(int id)
    {
        try
        {
            if (ModelState.IsValid)
            {
                domainUser GetAllUsers = new domainUser();

                using (var db = new DAL.HelpDeskContext())
                {


                    var query = (from m in db.Requests
                                 where m.ID == id
                                 select new editRequestViewModel()
                                 {

                                 userRequest = m,


                                 Tags = (from x in db.Tags
                                             select new SelectListItem()
                                            {
                                                Text = x.Name,
                                                Value = x.ID.ToString()
                                            }).ToList().OrderBy(x => x.Text).ToList(),

                                 SelectedTags =  (from x in db.AssociatedTags
                                                      join u in db.Tags on x.TID equals u.ID into cc
                                                      from c in cc.DefaultIfEmpty()
                                                      where x.RID == id
                                                      select new SelectListItem()
                                                      {
                                                        Text = c.Name,
                                                        Value = c.ID.ToString()

                                                      }).ToList().OrderBy(x => x.Text).ToList()
                                 }).FirstOrDefault();

                    return View(query);
                }
            }

            else
                return View();
        }
        catch (Exception ex)
        {
            return View("Error", new HandleErrorInfo(ex, "Change", "Browse"));
        }
    }

I have tried setting the value in the controller like this but it doesn't change the outcome. (15 is a possible ID for a tag)

query.SelectedTags[0].Value = "15";

And finally here is how I am using the drop down list in the View.

 <div class="form-group">
            @Html.Label("Tag One", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-2">
                @Html.DropDownListFor(model => model.SelectedTags[0].Value, Model.Tags, "Select Tag", new { @class = "form-control " })
            </div>

        </div>

Using the above code, the drop down list is defaulted to "Select Tag". Obviously if I have a value pulled into SelectedTags[0].Value, I want that value to be the default value when the page is first loaded.

Something is telling me the way its indexed is the issue, but I cannot seem to figure out why. Since I have 3 select list item objects in my list, I should be able to just specify the index, correct?

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Night Channels
  • 342
  • 1
  • 4
  • 12
  • Have you tried removing "Select Tag"? – Reyan Chougle Feb 06 '18 at 18:49
  • @ReyanChougle Yes. That simply removes "Select Tag" from being the first option, and instead selects the next item at the top of the list. – Night Channels Feb 06 '18 at 18:52
  • I believe your whole use of DropDownListFor is incorrect, as the first parameter is supposed to point the property you will store the selected value in, please look at this post from SO on how to use it properly, this also shows how to set the selected property of your drop down: (https://stackoverflow.com/questions/19476530/html-dropdownlistfor-selected-value-not-being-set) – Ryan Wilson Feb 06 '18 at 18:52
  • @RyanWilson I have used this method before with success. When the form is posted, it takes the selected values of each of the 3 dropdowns and stores them in the respective index specified in the helper. I dont see a problem with using SelectedTags to pull in any current selected tags, then collecting the new selected tags and reusing the list in the post method. – Night Channels Feb 06 '18 at 18:56
  • 1
    The only difference between this instance and others is I am using a collection as the binder – Night Channels Feb 06 '18 at 18:57
  • @NightChannels Did you even bother reading the post I added a link for? – Ryan Wilson Feb 06 '18 at 19:01
  • @RyanWilson Of course, and I read that while trying to solve before posting. – Night Channels Feb 06 '18 at 19:03
  • Is there only ever going to be three `SelectedTags`? You might have an easier time of it by making three properties on your view model that refer to your indexed values. – Heretic Monkey Feb 06 '18 at 19:03

1 Answers1

0

Following the guidance of @MikeMcCaughan and @RyanWilson I think I see the workable solution.

I added 3 properties into my viewmodel.

In the controller I then used the SelectedTags query to set each of those values to the possible 3 tag ID's that it could be.

query.tagSelOne = query.SelectedTags[0].Value;

And simply changed the Helper to:

@Html.DropDownListFor(model => model.tagSelOne, Model.Tags)

This seems to do the job.

Night Channels
  • 342
  • 1
  • 4
  • 12