I have the following 3 classes (all code is now included) ...
using System.ComponentModel.DataAnnotations;
namespace MyWebApp.Models
{
public class ApplicationUser
{
[Display(Prompt = "Your Name", Name = "Contact Name"), Required(), StringLength(255, MinimumLength = 3, ErrorMessage = "Please enter a valid contact")]
public string ContactName { get; set; }
[Display(Name = "Username", Prompt = "Login As"), Required(), StringLength(255, MinimumLength = 3, ErrorMessage = "Your username must be at least 3 characters")]
public string UserName { get; set; }
}
public class Account
{
[Display(Name = "Account Type", Prompt = "Account Type"), Required()]
public int AccountType { get; set; }
[Display(Name = "Organisation Name", Prompt = "Name of Organisation"), StringLength(255)]
public string OrganisationName { get; set; }
}
public class RegisterViewModel
{
public ApplicationUser ApplicationUser { get; set; }
public Account Account { get; set; }
public RegisterViewModel()
{
ApplicationUser = new ApplicationUser();
Account = new Account();
}
[Display(Name = "Password", Prompt = "Password"), Required(), DataType(DataType.Password), StringLength(255, MinimumLength = 5, ErrorMessage = "The password must be at least 7 characters")]
public string Password { get; set; }
[Display(Name = "Confirm Password", Prompt = "Confirm Password"), DataType(DataType.Password), Compare("Password", ErrorMessage = "Your confirmation doesn't match...")]
public string PasswordConfirmation { get; set; }
}
}
My controller is as follows:
using System.Web.Mvc;
using MyWebApp.Models;
namespace MyWebApp.Controllers
{
[Authorize]
public class AccountController : Controller
{
[AllowAnonymous]
public ActionResult Register()
{
RegisterViewModel mdl = new RegisterViewModel();
return View(mdl);
}
[HttpPost]
[AllowAnonymous]
public ActionResult Register([Bind(Include = "Account.AccountType, ApplicationUser.ContactName, ApplicationUser.UserName, Password, Account.OrganisationName, Account.OrganisationRegistrationNumber, Account.AddressLine1, Account.AddressLine2, Account.AddressLine3, Account.City, Account.County, Account.Postcode, ApplicationUser.Email, ApplicationUser.PhoneNumber, PasswordConfirmation")]RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = model.ApplicationUser;
var associatedAccount = model.Account;
var u1 = Request.Form["ApplicationUser.UserName"];
if (u1 == user.UserName)
{
ViewBag.Message = "We got it";
}
else
{
ViewBag.Message = "We failed!";
}
return View(model);
}
return View(model);
}
}
}
my view of Register.cshtml looks like this ...
@using MyWebApp.Models
@model MyWebApp.Models.RegisterViewModel
@{
ViewBag.Title = "Register User";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>New Account</h4>
<hr />
<p>@ViewBag.Message</p>
@Html.ValidationSummary(true)
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(model => model.ApplicationUser.ContactName, new { @class = "control-label col-md-5" })
<div class="col-md-7">
@Html.EditorFor(model => model.ApplicationUser.ContactName, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ApplicationUser.ContactName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ApplicationUser.UserName, new { @class = "control-label col-md-5" })
<div class="col-md-7">
@Html.EditorFor(model => model.ApplicationUser.UserName, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ApplicationUser.UserName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, new { @class = "control-label col-md-5" })
<div class="col-md-7">
@Html.EditorFor(model => model.Password, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Password)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PasswordConfirmation, new { @class = "control-label col-md-5" })
<div class="col-md-7">
@Html.EditorFor(model => model.PasswordConfirmation, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PasswordConfirmation)
</div>
</div>
</div>
<div class="col-md-6">
<div class="org-info">
<div class="form-group">
@Html.LabelFor(model => model.Account.OrganisationName, new { @class = "control-label col-md-5" })
<div class="col-md-7">
@Html.EditorFor(model => model.Account.OrganisationName, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Account.OrganisationName)
</div>
</div>
</div>
<hr />
<div class="form-group">
<div class="col-md-8"></div>
<div class="col-md-4">
<input type="submit" value="Sign Up" class="btn btn-default form-control" />
</div>
</div>
</div>
</div>
}
My problem is that when the user clicks "Register" without entering any information in the view, only the password and password confirmation fields are displayed as having problems, none of the account properties or ApplicationUser show the problem.
If I enter the details for the password and confirmation fields and place a breakpoint in the Register method, the ModelState.IsValid value will be true even if I have not added any user or user information.
If I log in, enter the username, rating ...
Request.Form["ApplicationUser.UserName"]
, ApplicationUser.UserName, , , Bind, !?
, Bind, UserName, ApplicationUser.UserName ApplicationUser_UserName, , , .
- , ( ), ?
Entity Framework 6
, , , , .