Html.DropDownList selected value not working using ViewBag

后端 未结 5 1099
谎友^
谎友^ 2021-01-05 20:10

Well, after a couple hours reading stuff over here, trying unsuccessfully all the solutions, also found this article that i thought it would save my life... nothing.

<
相关标签:
5条回答
  • 2021-01-05 20:37

    I know its an old question, but I will share another solution since sometimes you have done anything correctly, but you can't see the selected value after a request and response, since your razor has bug maybe. In this condition, that I've had, you can use select-option using viewbag:

    In controller I had:

     ViewBag.MyData = GetAllData(strSelected);
    ... 
    

    and as a private function in controller I had:

      private List<SelectListItem> GetAllData(List<string> selectedData)
        {
    
            return MyGetAll().Select(x =>
                new SelectListItem
                {
                    Text = x.Point,
                    Value = x.Amount.ToString(),
                    Selected = selectedPrizes.Contains(x.Amount.ToString())
                })
                .ToList();
        }  
    

    And in .cshtml I had:

    <select id="Amount" name="Amount" multiple="multiple">
      @foreach (var listItem in (List<SelectListItem>)ViewBag.MyData)
       {
         <option value="@listItem.Value" @(listItem.Selected ? "selected" : "")>
           @listItem .Text
         </option>
       }
    </select>
    

    I hope it will be useful for similar problems :)

    0 讨论(0)
  • 2021-01-05 20:46

    The first place i should've gone to learn.... and i didn't.

    Here is my solution

    Thank you thou for your answers. Really appreciate it. I feel like shooting my own foot now. Two hours reading blogs and it took me 2 minutes to replicate this example and worked perfectly...

    0 讨论(0)
  • 2021-01-05 20:58

    Change the name of the ViewBag property to NOT match the name of the DropDownList. Use ViewBag.yearDropDownDD instead of ViewBag.yearDropDown.

    ALSO, you are creating the SelectList wrongly. You need to pass the selected value not the array item:

    ViewBag.yearDropDown = new
    SelectList(years, "Value", "Text", years.Where(x => x.Selected).FirstOrDefault().Value);
    
    0 讨论(0)
  • 2021-01-05 20:59
    ViewBag.yearDropDown = new SelectList(years, "Value", "Text", years.Where(x => x.Selected).FirstOrDefault());
    

    Last parameter here is SelectListItem, but it must be selected value (string in your example)

    SelectList Constructor (IEnumerable, String, String, Object)

    0 讨论(0)
  • 2021-01-05 21:00

    The problem may also be the name, see here.

    In the Controller

    ViewBag.PersonList= new SelectList(db.Person, "Id", "Name", p.PersonId); 
    

    In the View

    @Html.DropDownList("PersonList",(SelectList)ViewBag.PersonList )
    

    This will not work, you have to change the name, so it's not the same.

    @Html.DropDownList("PersonList123",(SelectList)ViewBag.PersonList )
    

    So change yearDropDown and it Will Work for you.

    Best regards Christian Lyck.

    0 讨论(0)
提交回复
热议问题