0

My register page requires a dropdownlist that is populated with names from the Customer table. I have included the Customer foreign key in my AspNetUsers table in order to then reference the Customernames, I've added this field in the registerViewModel and IdentityModel. However not quite sure how to access CustomerNames using the CustomerId in the register form as a dropdownlist. Below is what I've tried:

AccountController:

public async Task<ActionResult> Register(RegisterViewModel model)
{

    if (ModelState.IsValid)
    {
        var user = new ApplicationUser { UserName = model.Email, Email = model.Email, CustomerID=model.CustomerID};
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CustomerCompanyName");
            return RedirectToAction("Index", "Home");
        }
        AddErrors(result);
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

View:

<div class="col-md-3">
    @Html.Label("Customer ID")
</div>
<div class="col-md-9">
    @Html.DropDownListFor(m => m.CustomerID, new SelectList(ViewBag.CustomerID, "CustomerID", "CustomerName"))
</div>
  • 1
    `ViewBag.CustomerID` is already a `IEnumerable`, so its `@Html.DropDownListFor(m => m.CustomerID, (IEnumerableViewBag.CustomerID)`, but that will not work correctly anyway - refer [this answer](http://stackoverflow.com/questions/37161202/will-there-be-any-conflict-if-i-specify-the-viewbag-name-to-be-equal-to-the-mode/37162557#37162557). ALWAYS use a view model, and in your case it will include `IEnumerable)CustomerList` and the view will be `@Html.DropDownListFor(m => m.CustomerID, Model.CustomerList` –  Aug 23 '17 at 10:19
  • Thanks @StephenMuecke, tell me if I am understanding correctly, should I create a class under Models that will contain all the names that I require in the dropdown? –  Aug 23 '17 at 10:42
  • No, In your `RegisterViewModel`, add a property `public IEnumerable CustomerList { get; set; }` . Refer both [this question and answer](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) for a typical example (and note you only set it in the POST method if `ModelState` is not valid - there is no point setting it if your redirecting as in your code) –  Aug 23 '17 at 10:46
  • Okay, so in your first comment, where do i now add this IEnumerable)CustomerList –  Aug 23 '17 at 11:02
  • As per my last comment (the `)` is a typo) - and look at the link I gave you previously –  Aug 23 '17 at 11:04

2 Answers2

0

you're in the wrong action method, put your viewbag in this Method instead:

[AllowAnonymous]
    public ActionResult Register()
    {
ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CustomerCompanyName");
            return View();
        }

Dropdown in view:

 @Html.DropDownListFor(model => model.CustomerID,
     (IEnumerable<SelectListItem>)@ViewBag.CustomerID , htmlAttributes: new
      {
          @class = "Form-control",

      })
0

@Unicorn_Girl,You should try this,

  ViewBag.CustomerID = new SelectList(db.Customers.Select(x => new KeyValuePair<string, string>(x, x)), "CustomerID", "CustomerCompanyName");

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

I hope this is useful

  • Create a 2nd identical `IEnumerable` from the 1st one is pointless extra overhead –  Aug 23 '17 at 23:33