How to create action and views for editing user custom informations?
Authorization is based on membership created by VS with MVC 4 project.
I\'ve added additiona
For adding additional properties to the UserProfile table you will need to change a series of changes in
Here in this example I am adding a new field called "MobileNumber", have declared it as type string.
[Table("UserProfiles")]
public class UserProfiles
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set;}
public string Email { get; set;}
public string FirstName { get; set; }
public string LastName { get; set; }
public string MobileNumber { get; set; }
}
Add the new property in your Model. In this example where we want an extra field while user registration we need to update the RegisterModel
class. Add validations if need be.
public class RegisterModel
{
[Required]
[Display(Name = "Email")]
[DataType(DataType.EmailAddress)]
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]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
[StringLength(11, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 11)]
[Display(Name = "Mobile No.")]
public string MobileNumber { get; set; }
}
Once you have update the Model you should be able to use the @Html.TextBoxFor(m => m.MobileNumber)
in your view, which will bind the MobileNumber
field to your 'MobileNumber' property declared in your model.
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
@Html.LabelFor(m = m.Email)
@Html.TextBoxFor(m => m.Email)
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.LabelFor(m => m.FirstName)
@Html.TextBoxFor(m => m.FirstName)
@Html.LabelFor(m => m.LastName)
@Html.TextBoxFor(m => m.LastName)
@Html.LabelFor(m => m.MobileNumber)
@Html.TextBoxFor(m => m.MobileNumber)
}
Last step, here you just need to read the value posted to your model and pass it along the repository for saving.
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
WebSecurity.CreateUserAndAccount(model.Email, model.Password,
new
{
FirstName = model.FirstName,
LastName = model.LastName,
MobileNumber = model.MobileNumber
});
WebSecurity.Login(model.Email, model.Password);
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
Now since your RegisterModel already has this property and so does your UserProfile class, using RegisterModel class on your view, you should be able to access the new added property.
Taken from here : How to add additional fields when using Membership in ASP.NET MVC 4