I have very little experience with razor and mvc controllers. I would like to add a dropdown box in my registration view. I have tried some different things from some examples online but I do not know how to access the class i need through the application user. I want to add a list of companies. I really got lost on going through the account viewmodels. i removed the relationship from the company class because i had to clean it up. not sure what is needed with this. a user can only have one company.
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity>
GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager
.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
Company Class
public class Company
{
public int CompanyId { get; set; }
public string CompanyName { get; set; }
public List<Document> Documents { get; set; }
}
Account View model
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required]
[Display(Name = "UserName")]
public string UserName { get; set; }
[Required]
[Display(Name = "CompanyName")]
public int CompanyId { get; set; }
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
//Property for the options
public IEnumerable<SelectListItem> CompanyOptions(string selected)
{
//This is just static - get it from somewhere else (database?)
return new List<SelectListItem>{
new SelectListItem{ Text = "Company 1", Value = "1", Selected = selected.Equals("1") },
new SelectListItem{ Text = "Company 2", Value = "2", Selected = selected.Equals("2") }
};
}
}
View
<div class="form-group">
@Html.LabelFor(model => model.CompanyId, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownFor(m => m.CompanyId, Model.CompanyOptions(Model.CompanyId), null)
</div>
Error Message
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'DropDownFor' and no extension method 'DropDownFor' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?) Source Error: Line 41: @Html.LabelFor(model => model.CompanyId, new { @class = "control-label col-md-2" }) Line 42: Line 43: @Html.DropDownFor(m => m.CompanyId, Model.CompanyOptions(Model.CompanyId), null) Line 44: Line 45: Blockquote
new error
Compiler Error Message: CS0121: The call is ambiguous between the following methods or properties: 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, System.Collections.Generic.IEnumerable, System.Collections.Generic.IDictionary)' and 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, System.Collections.Generic.IEnumerable, string)'
UserAdmin Controller
// GET: /Users/Create
public async Task<ActionResult> Create()
{
//Get the list of Roles
ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name");
return View();
}
// POST: /Users/Create
[HttpPost]
public async Task<ActionResult> Create(RegisterViewModel userViewModel, params string[] selectedRoles)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
UserName = userViewModel.UserName,
Email = userViewModel.Email,
CompanyId = userViewModel.CompanyId,
Name = userViewModel.Name
};
user.UserName = userViewModel.UserName;
user.Email = userViewModel.Email;
user.CompanyId = userViewModel.CompanyId;
user.Name = userViewModel.Name;
// Then create:
var adminresult = await UserManager.CreateAsync(user, userViewModel.Password);
//Add User to the selected Roles
if (adminresult.Succeeded)
{
if (selectedRoles != null)
{
var result = await UserManager.AddToRolesAsync(user.Id, selectedRoles);
if (!result.Succeeded)
{
ModelState.AddModelError("", result.Errors.First());
ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name");
return View();
}
}
}
else
{
ModelState.AddModelError("", adminresult.Errors.First());
ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");
return View();
}
return RedirectToAction("Index");
}
ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");
return View();
}
Account Controller
// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register()
{
return View();
}
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, CompanyId = model.CompanyId, Name = model.Name };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account",
new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id,
"Confirm your account",
"Please confirm your account by clicking this link: <a href=\""
+ callbackUrl + "\">link</a>");
ViewBag.Link = callbackUrl;
return View("DisplayEmail");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
Register View Model
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required]
[Display(Name = "UserName")]
public string UserName { get; set; }
[Required]
[Display(Name = "CompanyName")]
public int CompanyId { get; set; }
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
//Property for the options
public IEnumerable<SelectListItem> CompanyOptions(int selected)
{
//This is just static - get it from somewhere else (database?)
return new List<SelectListItem>{
new SelectListItem{ Text = "Company 1", Value = "1", Selected = selected.Equals("1") },
new SelectListItem{ Text = "Company 2", Value = "2", Selected = selected.Equals("2") }
};
}
}
The second error message is as a result of using null
for the 3rd parameter of DropDownListFor()
. The call is ambiguous because the is an overload that accepts string labelOption
and one that accepts object htmlAttributes
Remove the 3rd parameter (or if you want an option label, make it a string
, for example string.Empty
, or "-Please select-"
)
You also should not set the Selected
property of SelectListItem
. @Html.DropDownListFor(m => m.CompanyId, ...
is binding to property CompanyId
so if the value of CompanyId
matches the value of one of the options, then that option will be selected (in your code above, if CompanyId=2
, then the second option will be selected). The value of the Selected
property is ignored. Instead, change CompanyOptions
to a property
public IEnumerable<SelectListItem> CompanyOptions { get; set; }
and in the controller
public ActionResult Create()
{
RegisterViewModel model = new RegisterViewModel();
model.CompanyId = 2; // set this if you want to display a specific company
ConfigureViewModel(model);
return View(model);
}
public ActionResult Create(RegisterViewModel model)
{
if(!ModelState.IsValid)
{
ConfigureViewModel(model);
return View(model);
}
// Save and redirect
}
private void ConfigureViewModel(RegisterViewModel model)
{
model.CompanyOptions = new List<SelectListItem>()
{
new SelectListItem() { Text = "Company 1", Value = "1" },
new SelectListItem() { Text = "Company 2", Value = "2" }
};
}
If your access the companies from a database, its more likely to be
model.CompanyOptions = new SelectList(db.Companies, "ID", "Name")