-1

I have three asp mvc model

Unit [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int UnitId { get; set; } [Required] [StringLength(25)] public string UnitName { get; set; } Bank

    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int BankId { get; set; }
    [Required]
    [StringLength(25)]
   public string BankName { get; set; } 

BankAccount

    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int BankAccountId { get; set; }
    [Required]
    public int BankId { get; set; }
    [Required]
    public int UnitId { get; set; }
    [Required]
    [StringLength(25)]
    public string BankAccountNumber { get; set; }
    public virtual Bank Bank { get; set; }
    public virtual Unit Unit { get; set; }

BankAccountsController

// GET: BankAccounts/Create
    public IActionResult Create()
    {
        ViewData["BankId"] = new SelectList(_context.Bank, "BankId", "Bank");
        ViewData["UnitId"] = new SelectList(_context.Unit, "UnitId", "Unit");
        return View();
    }

**In Create View VS 2015 Scaffolding Code **

    <div class="form-group">
        <label asp-for="UnitId" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <select asp-for="UnitId" class="form-control"></select>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="BankId" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <select asp-for="BankId" class ="form-control"></select>
        </div>
    </div>

Dropdown List is empty How I can solve it?

  • This question might have answer here: http://stackoverflow.com/a/12091082/2270340. You are not associating the `ViewData` with `select`. – Sayan Pal Jun 12 '16 at 04:37
  • Possible duplicate of [Populating a dropdown from ViewData](http://stackoverflow.com/questions/12090937/populating-a-dropdown-from-viewdata) – Lhooq Jun 12 '16 at 04:39
  • 1
    Please use [ViewModels](http://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-3), is so much cleaner and easier to debug code. – Erik Philips Jun 12 '16 at 05:06

2 Answers2

1

I think the problem is that the association between the ViewData and the select list is missing.

As can be seen here, I think you need to use asp-items to bind the ViewData with the select list.

<select asp-for="UnitId" class="form-control" asp-items="HereGoesViewDataOrViewBagForUnits"></select>
Sayan Pal
  • 4,768
  • 5
  • 43
  • 82
0

Controller New Code

//ViewData["BankId"] = new SelectList(_context.Bank, "BankId", "Bank");
//ViewData["UnitId"] = new SelectList(_context.Unit, "UnitId", "Unit");
ViewData["BankId"] = new SelectList(_context.Set<Bank>(), "BankId", "BankName");
ViewData["UnitId"] = new SelectList(_context.Set<Unit>(), "UnitId", "UnitName");

New View Code

<select class="form-control"
                    asp-for="UnitId"
                    asp-items="ViewBag.UnitId"></select>
<select class ="form-control" 
                    asp-for="BankId" 
                    asp-items="ViewBag.BankId"></select>