gpt4 book ai didi

c# - 仅更新模型的一部分,验证问题?

转载 作者:太空宇宙 更新时间:2023-11-03 22:19:50 25 4
gpt4 key购买 nike

我有一个注册页面,用户可以在其中输入他的名字、姓氏、电子邮件和密码以及其他字段。

我已将验证属性绑定(bind)到此模型(称为“用户”并通过 LINQtoSQL 创建)并且一切正常。

模型代码:

[MetadataType(typeof(UserValidation))]
public partial class User { }

[Bind(Exclude = "UserID")]
[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirm password don't match.")]
public class UserValidation
{
[Required(ErrorMessage = "First name required"), StringLength(20, MinimumLength=3, ErrorMessage = "Must be between 3 and 20 characters")]
public string FirstName { get; set; }

[Required(ErrorMessage = "Last name required"), StringLength(20, MinimumLength = 3, ErrorMessage = "Must be between 3 and 20 characters")]
public string LastName { get; set; }

[Required(ErrorMessage = "Email address required"), RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage = "Must be a valid email address")]
public string Email { get; set; }

[Required(ErrorMessage = "Password required"), StringLength(20, MinimumLength = 6, ErrorMessage = "Password must be between 6 and 20 characters")]
public string Password { get; set; }

[Required(ErrorMessage = "Confirm password required"), StringLength(20, MinimumLength = 6, ErrorMessage = "Password must be between 6 and 20 characters")]
public string ConfirmPassword { get; set; }

}

注册和登录后,我希望用户能够在一个 View 中编辑他们的名字、姓氏和电子邮件(我们称这些“帐户”字段),并在另一个 View 中编辑“密码”。这就是我的问题所在。

当我通过注册时使用的同一模型(“用户”)提交更新帐户字段数据的表单时,IsValid 方法会针对缺少的“密码”字段显示 ModelState 错误。

Controller 代码:

    //
// GET /Talent/Account
public ActionResult Account()
{
string cookieUser = User.Identity.Name;
User user = userRepository.GetUserByEmail(cookieUser);
return View(user);
}

// POST /Talent/Account
[HttpPost]
public ActionResult Account(User model)
{

if (ModelState.IsValid)
{
// do something
ModelState.AddModelError("", "All good.. "+ model.FirstName + " - " + model.LastName);
}

return View(model);
}

我该如何解决这个问题??最佳实践等...

最佳答案

以模型- View - View 模型 (MVVM) 方式,您应该创建两个 ViewModel:一个用于创建新帐户,另一个用于编辑名称字段而不需要密码。

将验证属性应用于您的 ViewModel,而不是直接应用于您的模型。

一些示例包装器 ViewModel 类:

        [Bind(Exclude = "UserID")]    
public class UserForAccountEdit
{
public User UserAccount { get; set; }

[Required(ErrorMessage = "First name required"), StringLength(20, MinimumLength=3, ErrorMessage = "Must be between 3 and 20 characters")]
public string FirstName
{
get
{ return UserAccount.FirstName };
set
{ UserAccount.FirstName = value; }
}

...
}



[Bind(Exclude = "UserID")]
[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirm password don't match.")]
public class UserForAccountCreation
{
public User UserAccount { get; set; }

[Required(ErrorMessage = "First name required"), StringLength(20, MinimumLength=3, ErrorMessage = "Must be between 3 and 20 characters")]
public string FirstName
{
get
{ return UserAccount.FirstName };
set
{ UserAccount.FirstName = value; }
}

...
}

关于c# - 仅更新模型的一部分,验证问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3557978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com