参与我的第一个MVC 3项目,但这似乎很简单。我有一个viewmodel类,其属性是另一个viewmodel。 (参见下面的代码)当我已经有一个拥有它的所有代码时,再次为其他视图模型重新创建所有相同的相同代码(验证/属性)似乎不正确。
当我打电话给编辑时,一切正常。表单显示我希望看到的值,但验证在“LoginId”远程验证上失败,因为LoginId为null。我无法弄清楚我做错了什么。
Customer类ValidateLogin方法中发生了故障。最后列在下面。
如果这不是最佳做法,请指出我正确的方向。
以下是相关代码。
模型
public class UserViewModel
{
[Display(Name = "Customer Id")]
public Guid CustomerId { get; set; }
[Required(ErrorMessageResourceType = typeof(UserErrorMessages), ErrorMessageResourceName = "FirstNameRequired")]
[StringLength(50, ErrorMessageResourceType = typeof(UserErrorMessages), ErrorMessageResourceName = "FirstNameStringLength")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required(ErrorMessageResourceType = typeof(UserErrorMessages), ErrorMessageResourceName = "LastNameRequired")]
[StringLength(50, ErrorMessageResourceType = typeof(UserErrorMessages), ErrorMessageResourceName = "LastNameStringLength")]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required(ErrorMessageResourceType = typeof(UserErrorMessages), ErrorMessageResourceName = "LoginIdRequired")]
[StringLength(100, ErrorMessageResourceType = typeof(UserErrorMessages), ErrorMessageResourceName = "LoginIdStringLength")]
[Remote("ValidateLogin", "Validation")]
[Display(Name = "Login")]
public string LoginId { get; set; }
[Required(ErrorMessageResourceType = typeof(UserErrorMessages), ErrorMessageResourceName = "PasswordRequired")]
public string Password { get; set; }
[Required(ErrorMessageResourceType = typeof(UserErrorMessages), ErrorMessageResourceName = "ConfirmPasswordRequired")]
[Compare("Password", ErrorMessageResourceType = typeof(UserErrorMessages), ErrorMessageResourceName = "ConfirmPasswordCompare")]
[Display(Name = "Confirm Password")]
public string ConfirmPassword { get; set; }
[Required(ErrorMessageResourceType = typeof(UserErrorMessages), ErrorMessageResourceName = "EmailRequired")]
//[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}")]
[StringLength(100, ErrorMessageResourceType = typeof(UserErrorMessages), ErrorMessageResourceName = "EmailStringLength")]
[Email(ErrorMessageResourceType = typeof(UserErrorMessages), ErrorMessageResourceName = "EmailInvalid")]
public string Email { get; set; }
}
public class CustomerAccountViewModel
{
public UserViewModel User { get; set; }
[Display(Name = "MI")]
[StringLength(1, ErrorMessageResourceType = typeof(CustomerAccountErrorMessages), ErrorMessageResourceName = "MiddleInitialStringLength")]
public string MiddleInitial { get; set; }
public IEnumerable<SelectListItem> SuffixList { get; set; }
[StringLength(5, ErrorMessageResourceType = typeof(CustomerAccountErrorMessages), ErrorMessageResourceName = "SuffixStringLength")]
public string Suffix { get; set; }
}
控制器
public ActionResult Edit(Guid id)
{
Customer customer = new Customer();
CustomerAccountViewModel model = new CustomerAccountViewModel();
// Get the temporary user record that was created.
model.User = customer.GetUserRegistration(id);
// Set the list of valid suffixes.
model.SuffixList = Utilities.CreateSuffixList().Select(s => new SelectListItem() { Value = s.ToString(), Text = s.ToString() });
return View(model);
}
查看
@model SteadyPay.Models.CustomerAccountViewModel
@using Resources;
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>CustomerAccountViewModel</legend>
Note: This calls the editor template (User.cshtml) listed below.
@Html.EditorFor(model => model.User)
<div class="editor-label">
@Html.Label(@CustomerAccountViewText.MiddleInitialDisplay)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MiddleInitial)
@Html.ValidationMessageFor(model => model.MiddleInitial)
</div>
<div class="editor-label">
@Html.Label(@CustomerAccountViewText.SuffixDisplay)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Suffix, Model.SuffixList, string.Empty, Model.Suffix)
@Html.ValidationMessageFor(model => model.Suffix)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
User.cshtml
@model SteadyPay.Models.UserViewModel
@using Resources;
@Html.ValidationSummary(true)
<fieldset>
<div class="editor-label">
@Html.Label(@UserViewText.FirstNameDisplay)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.Label(@UserViewText.LastNameDisplay)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.Label(@UserViewText.LoginIdDisplay)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LoginId)
@Html.ValidationMessageFor(model => model.LoginId)
</div>
<div class="editor-label">
@Html.Label(@UserViewText.PasswordDisplay)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.Label(@UserViewText.ConfirmPasswordDisplay)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ConfirmPassword)
@Html.ValidationMessageFor(model => model.ConfirmPassword)
</div>
<div class="editor-label">
@Html.Label(@UserViewText.EmailDisplay)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
</fieldset>
远程验证器
public class ValidationController : Controller
{
// NOTE: The parameter name(s) have to match the property name envoking the validation,
// and any name(s) in the AdditionalFields parameter of the Remote definition.
public JsonResult ValidateLogin(string loginId)
{
Customer customer = new Customer();
bool validLogin = customer.ValidateLogin(loginId);
return Json(validLogin, JsonRequestBehavior.AllowGet);
}
}
客户类代码 这是代码失败的地方,loginId为空
public bool ValidateLogin(string loginId)
{
// TODO: Add logic to validate if the LoginId is unique.
bool uniqueLogin = !loginId.Equals("khahn");
return uniqueLogin;
}