MVC membership - registration with dropdown datatype
Hi,
I'm dipping into Umbraco MVC, Umbraco 6.1.6, trying to create a membership registration/log-in system. I'm using Warren Buckley's 'Standard Membership' nuget package as a base, and it's working nicely. However, I want to add an additional registration field called 'profession' which will be a dropdown list from which the member makes a selection upon registering.
I've created a new datatype of type 'Dropdown' and have populated it with a couple of options. I've added this datatype to the member type. Now I'd like to pull in the options from this dropdown into the registration form but I'm struggling to find out how to do this.
Here's the model for the registration form:
public class RegisterViewModel
{
[Required(ErrorMessage = "Please enter yourname")]
public string Name { get; set; }
/*
Not sure what type the dropdown should be here:
[DisplayName("Profession")]
[Required(ErrorMessage = "Please enter your profession")]
public ListItemCollection Professions { get; set; }
public string Profession { get; set; }
*/
[DisplayName("Email address")]
[Required(ErrorMessage = "Please enter your email address")]
[EmailAddress(ErrorMessage = "Please enter a valid email address")]
[Remote("CheckEmailIsUsed", "AuthSurface", ErrorMessage = "The email address has already been registered")]
public string EmailAddress { get; set; }
[UIHint("Password")]
[Required(ErrorMessage = "Please enter your password")]
public string Password { get; set; }
[UIHint("Password")]
[DisplayName("Confirm Password")]
[Required(ErrorMessage = "Please enter your password")]
[EqualTo("Password", ErrorMessage = "Your passwords do not match")]
public string ConfirmPassword { get; set; }
}
And here's part of the view which renders the registration form:
I'm not sure how or where to bind the values to HTML select element options and I've not been able to pull this together from the documentation or various blog posts I've looked at.
If anyone could point me in the right direction I'd be most grateful.
So then you need to populate the SelectList of professions from your controller action that renders this partial view, via something like this:
[ChildActionOnly]
public PartialViewResult Register()
{
return PartialView("Register", new RegisterViewModel
{
Professions = GetSelectListForProfessions(),
});
}
private SelectList GetSelectListForProfessions()
{
var professionsList = new List<string>();
var pv = umbraco.library.GetPreValues(1000); // 1000 being your drop down data type Id
while (pv.MoveNext())
{
professionsList.Add(pv.Current.Value);
}
return new SelectList(professionsList);
}
Caveating that I haven't had chance to test this, butI believe that'll do it - though there's a couple things I'm not that happy about. Firstly the hard-coded Id for the data type. At least you should put that into a config file, but ideally there would be a look-up by data type name. Not sure if that exists as an API call. And then the method GetSelectListForProfessions() you can see goes from an XPathNodeIterator, to a List<string> to a SelectList. Seems a bit around the houses so you might find a better way.
MVC membership - registration with dropdown datatype
Hi,
I'm dipping into Umbraco MVC, Umbraco 6.1.6, trying to create a membership registration/log-in system. I'm using Warren Buckley's 'Standard Membership' nuget package as a base, and it's working nicely. However, I want to add an additional registration field called 'profession' which will be a dropdown list from which the member makes a selection upon registering.
I've created a new datatype of type 'Dropdown' and have populated it with a couple of options. I've added this datatype to the member type. Now I'd like to pull in the options from this dropdown into the registration form but I'm struggling to find out how to do this.
Here's the model for the registration form:
And here's part of the view which renders the registration form:
I'm not sure how or where to bind the values to HTML select element options and I've not been able to pull this together from the documentation or various blog posts I've looked at.
If anyone could point me in the right direction I'd be most grateful.
Many thanks.
Hi Dan
I'd suggest the field in your view model should look something like this:
And then your view would be this:
So then you need to populate the SelectList of professions from your controller action that renders this partial view, via something like this:
Caveating that I haven't had chance to test this, butI believe that'll do it - though there's a couple things I'm not that happy about. Firstly the hard-coded Id for the data type. At least you should put that into a config file, but ideally there would be a look-up by data type name. Not sure if that exists as an API call. And then the method GetSelectListForProfessions() you can see goes from an XPathNodeIterator, to a List<string> to a SelectList. Seems a bit around the houses so you might find a better way.
Hope that helps.
Andy
is working on a reply...