So I thought this would be extremely simple, but I'm having a hard time figuring it out.
First, I'll go over what I have so far.
The RegisterViewModel:
public class RegisterViewModel
{
[Display(Name = "First Name:")]
public string FirstName { get; set; }
[Display(Name = "Last Name:")]
public string LastName { get; set; }
[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; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Confirm password:")]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Display(Name = "Please choose the category that best describes your professional relation to the field of Orthotics and Prosthetics:")]
public List<string> ProfessionalRelations { get; set; }
[Display(Name = "Please check all of the following that apply to you:")]
public List<string> UserRelations { get; set; }
[Required]
[Display(Name = "Country")]
public SelectList Countries { get; set; }
[Required]
[Display(Name = "EDGE Direct E-mail Newsletter:")]
public List<string> EdgeNewsletter { get; set; }
}
Notice the SelectList for Countries.
Now, in my controller. When someone goes to the Register page, I populate the Country Select List as follows:
var countries = oandpService.GetCountries().OrderBy(x => x.CountryName).ToList();
var usa = countries.FirstOrDefault(x => x.CountryID == "US");
countries.Remove(usa);
countries.Insert(0, new Country() { CountryID = string.Empty, CountryName = string.Empty });
countries.Insert(1, usa);
rvm.Countries = new SelectList(countries, "CountryID", "CountryName");
Finally, here's a part of the view for the countries DropDownListFor:
@Html.DropDownListFor(x => x.Countries, Model.Countries, Model.Countries)
This all displays fine when you go to the page. Countries drop down is generated with the CountryID as the value and CountryName as the Display Text.
Then, just for testing the radio buttons, checkboxes, and dropdowns I have the following:
[HttpPost]
[ValidateAntiForgeryToken]
[ReCaptchaFilter]
public ActionResult Register(RegisterViewModel rvm)
{
ViewBag.Countries = rvm.Countries.SelectedValue;
ViewBag.UserRelation = rvm.UserRelations;
ViewBag.ProfessionalRelation = rvm.ProfessionalRelations;
return View(rvm);
}
Except I receive the following errors on post:
- No parameterless constructor defined for this object.
- No parameterless constructor defined for this object. Object type 'System.Web.Mvc.SelectList'.
Could anyone help point me in the right direction? I've done some research but haven't figured it out. Any input would be greatly appreciated.