1

I have a linq query that is binded to a @Html.dropdownfor. If I assign a value to the user's account and leave the page. When I go back to the page, It's default value or preselected value is "Choose a schedule" even when I have a value assigned to it from before. I need it to preselect to the value already stored in the database. It was preselecting correctly with this

 ScheduleBuilderList = _context.ScheduleBuilder.ToList()

until I binded LINQ query. How can I get the database value to the be default value selected in dropdown without messing up the LINQ?

   @Html.DropDownListFor(m => m.ApplicationUser.ScheduleSum, new SelectList(Model.ScheduleBuilderList, "ScheduleSummary", "ScheduleSummary"), "Choose a schedule", new {@class = "form-control", id="dropdown"})

LINQ

ScheduleBuilderList = _context.ScheduleBuilder.Where(s => s.IsScheduleAssigned == false).ToList()

enter image description here

enter image description here

Full Actionresult code with recommended change below

 public ActionResult  EmployeeDetails(string id, ScheduleBuilder scheduleBuilder, ApplicationUser applicationUser)
{
    var user = _context.Users.SingleOrDefault(e => e.Id == id);

    if (user == null)
    {
        return HttpNotFound();
    }

    var viewModel = new TierViewModel()
    {
        ApplicationUser = user,
        TierLevel = _context.Tier.ToList(),
        ScheduleBuilderList = _context.ScheduleBuilder
            .Where(s => s.IsScheduleAssigned == false)
            .Select(item => new SelectListItem
            {
                Text = item.ScheduleSummary,
                Value = item.ScheduleSummary,
                Selected = item.ScheduleSummary == user.ScheduleSum
            })

    };

    var user1 = _context.Users.ToList();

    var schedule1 = _context.ScheduleBuilder.ToList();

    return View("EmployeeDetails", viewModel);
}
2
  • Your new linq query is filtering the value stored in database. You might want .Where(s => s.IsScheduleAssigned == false || s == currStoredSchedule). This is just pseudo code because we don't know how your ScheduleBuilderList look like.. Commented Jun 4, 2017 at 15:51
  • Setting the Selected value of SelectListItem is ignored when you bind to a model property using DropDownListFor(). Delete the Selected = item.ScheduleSummary == user.ScheduleSum line - its pointless. Commented Jun 4, 2017 at 21:57

1 Answer 1

1

You could try something like this:

var ScheduleBuilderList = _context.ScheduleBuilder
                                  .Where(s => s.IsScheduleAssigned == false)
                                  .Select(item => new SelectListItem
                                  {
                                      Text = item.ScheduleSummary,
                                      Value = item.ScheduleSummary,
                                      Selected = item.ScheduleSummary == "usersvalue"
                                  });

Where the literal "usersvalue" should be replaced the value of ApplicationUser.ScheduleSum for which you are interested in. Then you should change a bit also your View, like below:

@Html.DropDownListFor(m => m.ApplicationUser.ScheduleSum, 
                      Model.ScheduleBuilderList, 
                      "Choose a schedule", 
                      new {@class = "form-control", id="dropdown"})

and your Model should replace the type of ScheduleBuilderList with the following type:

IEnumerable<SelectListItem>
Sign up to request clarification or add additional context in comments.

11 Comments

I don't think I can declare ScheduleBuilderList because it's in viewmodel. For clarification I posted the full ActionResult method above with a screenshot of your suggested code. ScheduleBuilderList in my viewModel is listed like public IEnumerable<ScheduleBuilder> ScheduleBuilderList { get; set; } to grab dropdown items.
@user6680 Great ! You should change this IEnumerable<ScheduleBuilder> ScheduleBuilderList { get; set; } to this IEnumerable<SelectListItem> ScheduleBuilderList { get; set; } inside your ViewModel definition.
@user6680 You have missed also one closing curly brace after the Selected = s.ScheduleSummary == user.ScheduleSum. You should add there this }
I fixed that part thanks, but I'm still getting errors. I updated the code and screenshots above. I'm guessing the second screenshot error is because of the first screenshot error. "Property ScheduleBuilderList is never used" is what I'm getting with this in viewmodel IEnumerable<SelectListItem> ScheduleBuilderList { get; set; }
@user6680 My bad...Sorry.It's a bit difficult writing code out of a code editor :( Silly error...instead of s you should use the item, since this is the name of the captured variable.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.