-1

I wanted to keep my value after a post back so I passed the value to the ViewBag in the controller. On my view i have something like

<select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);" name="" id="" class="filter-select">
    <option selected value="">Topic</option>
    @{string selectedOption = ViewBag.previous;

        foreach (var topic in ViewBag.topicList)
        {
        <option value="/resources/categories/@topic" selected="@(selectedOption == topic ? "selected" : "")" >@topic</option>
        }
     }
</select>

however the selected value always ends up being the last value no matter what. Even when the viewbag is empty. Is there any way around this?

2
  • is there any reason you are not using Html helpers to populate dropdownlist? Commented Apr 18, 2018 at 15:51
  • I'm pretty new to MVC, if that's an easier route to go then I am all for it. Know of any good examples I can use to model my code from?
    – DanielJ
    Commented Apr 18, 2018 at 15:54

1 Answer 1

1

If you put selected in your option, it is going to register as selected whether or not you assign a value to it. So what you should do is only give your option the selected attribute if it meets your criteria.

One way to do this would be:

<select onchange = "this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);" name="" id="" class="filter-select">
    <option value="">Topic</option>
    @{string selectedOption = ViewBag.previous;

        foreach (var topic in ViewBag.topicList)
        {
            <option value = "/resources/categories/@topic" @if(selectedOption == topic) { @Html.Raw("selected"); } >@topic</option>
        }
    }
</select>

Just as a final note, it's generally preferred to use other methods than ViewBag for passing data between your Controller and View. The method I tend to use is to create a ViewModel that contains all the information the view needs, and I pass it that way.

7
  • Thanks but this doesn't seem to work. It goes back to the first value, topic.
    – DanielJ
    Commented Apr 18, 2018 at 16:14
  • Remove the selected option from the first value. Then check to make sure that the condition selectedOption == topic is returning true when you want it to.
    – Chris
    Commented Apr 18, 2018 at 16:20
  • Tried that, the condition is met but its still defaulting to topic for some reason
    – DanielJ
    Commented Apr 18, 2018 at 16:25
  • Have you checked the html after the page loads to see if the element you expect has the selected attribute?
    – Chris
    Commented Apr 18, 2018 at 16:32
  • 1
    My apologies Chris. Turns out I was missing the @ before Html.Raw("selected") and it turns out it is indeed needed despite the earlier @. I added that in and all appears to be working now. thanks!
    – DanielJ
    Commented Apr 18, 2018 at 16:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.