-2

I have dropdown value that is populated using viewBag in controller.

ViewBag.Excursion = excursions_List.ResultList.Select(x => new SelectListItem()
{
      Text = x.ExcursionName,
      Value = x.ExcursionName,
});

Following is the code that I used in view. For this scenario I have value for model.Excursion. But its not getting selected in the dropdown value. Dropdown is still showing empty value.

                       @Html.DropDownListFor(model => model.Excursion, new SelectList(ViewBag.Excursion, "Value", "Text"), " ", new { @class = "form-control" })

Can anybody point out what am I doing here wrongly?

Thanks.

Kate Fernando
  • 381
  • 1
  • 4
  • 18
  • 1
    If the value of `IssuedFor` matches one of the option values, then that option will be selected. And as a side note, creating an identical `IEnumerable` from the first one using `new SelectList(..)` is pointless extra overhead. Just use `@Html.DropDownListFor(model => model.IssuedFor, new (IEnumerable)ViewBag.PackageList, " ", new { id = "issuedFor", @class = "form-control" })` –  Aug 10 '17 at 04:57
  • Refer these questions: https://stackoverflow.com/questions/11042660/asp-net-mvc-dropdownlistfor-not-selecting-value-from-model & https://stackoverflow.com/questions/16799476/mvc-dropdownlistfor-not-selecting-value-from-model. – Tetsuya Yamamoto Aug 10 '17 at 04:58
  • 1
    Also adding `new { id = "issuedFor" }` is not necessary - the method already generates `id="IssuedFor"` –  Aug 10 '17 at 05:01
  • @StephenMuecke, thanks. For some dropdown values above code is working fine. But in the same view some dropdown values are not showing correctly. It is showing empty value. Still I am trying to figure out the issue.Do I have to set selected option in controller too? – Kate Fernando Aug 10 '17 at 05:37
  • It will display correctly if the value of `IssuedFor` matches one of the option values - either you have not set the value of `IssuedFor` before you passed the model to the view, of it does not match exactly. Either that or there is some other code you have not shown us, for example this is in an `EditorTemplate` and your using a collection –  Aug 10 '17 at 05:39
  • Ensure also that `IssuedFor` is actually a property and not just a field in your model. –  Aug 10 '17 at 06:41
  • 1
    Have just seen your edit. You cannot have the same name for the property your binding to and the `SelectList`. Refer [Can the ViewBag name be the same as the Model property name in a DropDownList?](https://stackoverflow.com/questions/37161202/can-the-viewbag-name-be-the-same-as-the-model-property-name-in-a-dropdownlist/37162557#37162557). Change the `ViewBag` name to say `ExcursionList` and it will work fine. But you should not be using `ViewBag` anyway. Your editing data so always use a view model and that model will contains a property `IEnumerable –  Aug 10 '17 at 08:04
  • Yes. You are right. I just found an answer from stackoverflow now. Thanks. This took me lot of time anyway to figure this out. – Kate Fernando Aug 10 '17 at 08:10
  • In future, post the real code that is causing the issue, not code which actually works so you do not waste the time of this community. –  Aug 10 '17 at 08:26
  • Thanks for the comment. Yes that's right. I understood it very well, since I already waste lot of time. – Kate Fernando Aug 10 '17 at 08:28

1 Answers1

0

Do this in your view:

@{
List<SelectListItem> packages = ViewBag.PackageList as List<SelectListItem>;
}

@Html.DropDownListFor(model => model.IssuedFor, packages, "Select a Package", new { id = "issuedFor", @class = "form-control" })

In the controller, it is better to work with ids:

ViewBag.PackageList = pkgList.ResultList.Select(x => new SelectListItem()
{
    Text = x.PackageName,
    Value = x.Id, //use the id
});

In case you are assign the id to IssuedFor as value and you are using the package name as Value in the dropdown, then it will not work. The Value in ddl and the IssuedFor should match

alaa_sayegh
  • 2,141
  • 4
  • 21
  • 37
  • That makes no difference at all! –  Aug 10 '17 at 06:11
  • @StephenMuecke I was not completely finsihed with my answer dear :) but thanks for the hint!! – alaa_sayegh Aug 10 '17 at 06:18
  • You do not know what OP wants to bind to! –  Aug 10 '17 at 06:20
  • @alaa_sayegh, I want to use the Value as x.PackageName too...My concern is this is work for the same view with some dropdown values. And I am using the same code for the working scenarios too. I just check the model data and view data too. They are same. But not showing the selected value in the drop down. – Kate Fernando Aug 10 '17 at 06:26
  • @KateFernando you need then to check the items in the ViewBag.PackageList (the packages) and also check the value of the IssuedFor. It should work if the value exists in the dropdown. Can you screenshot the IssuedFor value together with the dropdown values. Important !! - You should also make sure that you dont have any other control in the form that has the same id "issuedFor" as the dropdown – alaa_sayegh Aug 10 '17 at 06:30
  • @alaa_sayegh, I have added the screenshots. Please refer Excursion property. – Kate Fernando Aug 10 '17 at 06:43
  • @KateFernando, what are the values in the IssuedFor Dropdown? Do they contain Holidays? – alaa_sayegh Aug 10 '17 at 06:54
  • @alaa_sayegh, Sorry for the confusion. Please refer updated question. – Kate Fernando Aug 10 '17 at 06:54
  • have you tried to remove the asterisk "*" ? Remove it from the text in dropdown and in Excursion Field. Maybe it is not happy with special characters – alaa_sayegh Aug 10 '17 at 07:00