ViewBag values from Database displaying as whiteout

37 Views Asked by At

Need some help with fixing values that are extracting in from DB via ViewBag dropdown binding.

Results are coming as whiteout, which means data is not visible in View.

Here is my Code

Class

[Key]
public int countryId { get; set; }    
            
public IEnumerable<SelectList> countryName { get; set; }
    
public string countryCode { get; set; }

Controller

public ActionResult DropDown()
        {

            using (Db db = new Db())
            {
                ViewBag.countryVal = new SelectList(db.Countries.ToList(), "countryName", "countryName");

                return View();
            }
        }

View

<table>
    <tbody>
    <td>
        @Html.DropDownList("countryVal", "Select")
    </td>
    </tbody>
</table>

Screenshot

NA Any help is very much appreciated.

Thanks

1

There are 1 best solutions below

0
Kiran Joshi On

Your countryName filed is invalid. You need to make it as string countryName as follows.

Country

[Key]
public int countryId { get; set; }    
            
public string countryName { get; set; }
    
public string countryCode { get; set; }

Your controller look like below:

public ActionResult DropDown()
        {

            using (Db db = new Db())
            {
                ViewBag.countryVal = new SelectList(db.Countries.ToList(), "countryName", "countryName");

                return View();
            }
        }

Now in UI need some changes as per below:

@Html.DropDownList("countryVal",ViewBag.countryVal as SelectList)