【发布时间】:2014-05-07 16:28:58
【问题描述】:
我有以下 3 个类(现在包括所有代码)...
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; }
}
}
我的控制器看起来像这样...
using System.Web.Mvc;
using MyWebApp.Models;
namespace MyWebApp.Controllers
{
[Authorize]
public class AccountController : Controller
{
// GET: /Account/
[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)
{
// we got it
ViewBag.Message = "We got it";
}
else
{
// no we didn't
ViewBag.Message = "We failed!";
}
return View(model);
}
// If we got this far, something failed, redisplay form
return View(model);
}
}
}
我的 Register.cshtml 视图如下所示...
@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>
}
我的问题是,当用户点击注册而不在视图中输入任何详细信息时,只有密码和密码确认字段显示有问题,帐户或应用程序用户属性均未显示问题。
如果我输入密码和确认字段的详细信息并在 Register 方法中放置一个断点,即使我没有添加任何帐户或用户详细信息,ModelState.IsValid 值也是 true。
如果我输入了用户名,正在评估...
Request.Form["ApplicationUser.UserName"]
给出了正确的值,但我认为应该由 Bind 填充的 ApplicationUser.UserName 什么都没有!?
我怀疑这是我的 Bind 声明,但我尝试了 UserName、ApplicationUser.UserName 和 ApplicationUser_UserName,但似乎都有同样的问题。
这个问题来自我提出的另一个问题(下面的链接),那么我错过了什么?
Entity Framework 6 Reusing Data Annotations
请注意,我对这个特定实现中的问题感兴趣,而不是出于各种我不想深入讨论的原因提供替代实现。
【问题讨论】:
-
你能展示一下你的观点吗?
-
我从来没有像你这样使用过带有嵌入式类的视图模型,但我会为
ApplicationUser和Account添加一个[Required]属性来做什么? -
请发表您的看法。我的概念证明在有和没有构造函数以及有和没有 Bind 属性的情况下都有效。它必须与您的视图以及您发布值的方式有关。
-
刚刚添加了我正在使用的视图字段示例和更多信息。
-
添加了完整的代码以允许人们复制和粘贴。
标签: c# asp.net-mvc entity-framework validation data-annotations