尝试验证并在控制器中手动设置值 (ASP MVC 3)

发布于 2024-12-08 11:34:43 字数 883 浏览 3 评论 0原文

我有一个供管理员和普通用户使用的“新用户”表单。两种形式都使用 RegisterModel

public class RegisterModel
{
    [Required]
    public string Name { get; set; }

    public string Email { get; set; }

    [Required]
    public string Password { get; set; }
}

区别在于,在我的前端“新用户”页面上,我希望用户提供自己的密码。但在后端,我希望系统生成密码。

由于我对两种表单使用相同的 RegisterModel,因此我在后端收到验证错误,提示 Password is required.

我想,我可以通过将其添加到我的控制器来解决这个问题:

    [HttpPost]
    public ActionResult New(RegisterModel model)
    {
        model.Password = Membership.GeneratePassword(6, 1);

        if (TryValidateModel(model))
        {
            // Do stuff
        }

        return View(model);
    }

但我仍然收到错误消息需要密码。。当我在控制器中调用 TryValidate 时,为什么会出现此问题?

对于此问题,最佳实践是什么?创建一个单独的 RegisterModelBackEnd 或是否有其他解决方案?

I have a "New user" form both for admins and for regular users. Both form use the RegisterModel

public class RegisterModel
{
    [Required]
    public string Name { get; set; }

    public string Email { get; set; }

    [Required]
    public string Password { get; set; }
}

The difference is that on my front end "New user" page I want users to provide their own password. But in back end, I want the system to generate the password.

Since I use the same RegisterModel for both forms, I get a validateion error in the back end saying Password is required..

I thought, I could solve this by adding this to my controller:

    [HttpPost]
    public ActionResult New(RegisterModel model)
    {
        model.Password = Membership.GeneratePassword(6, 1);

        if (TryValidateModel(model))
        {
            // Do stuff
        }

        return View(model);
    }

But I still get the error message Password is required.. Why is this the issue when I do call TryValidate in my controller?

What would be best practice for this issue, create a separate RegisterModelBackEnd or are there any other solutions to this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百思不得你姐 2024-12-15 11:34:43

手动更新模型时,不需要将其用作Action中的参数。另外,使用此重载,它允许您仅指定绑定的属性会发生。

protected internal bool TryUpdateModel<TModel>(
    TModel model,
    string[] includeProperties
)
where TModel : class

因此,工作代码将是

[HttpPost]
public ActionResult New()
{
    RegisterModel model = new RegisterModel();
    model.Password = Membership.GeneratePassword(6, 1);

    if (TryValidateModel(model, new string[] {"Name", "Email"}))
    {
        // Do stuff
    }

    return View(model);
}

您可以使用 BindAttribute

[HttpPost]
public ActionResult New([Bind(Exlude="Password")]RegisterModel model)
{
    if(ModelState.IsValid)
    {
       model.Password = Membership.GeneratePassword(6, 1);
       // Do Stuff
    }

    return View(model);
}

最后是最简单且最好的方法

定义单独的视图模型

When updating model manually, you do not need to use it as parameter in Action. Also, use this overload that lets you specify only the properties on which binding will occur.

protected internal bool TryUpdateModel<TModel>(
    TModel model,
    string[] includeProperties
)
where TModel : class

So, the working code will be

[HttpPost]
public ActionResult New()
{
    RegisterModel model = new RegisterModel();
    model.Password = Membership.GeneratePassword(6, 1);

    if (TryValidateModel(model, new string[] {"Name", "Email"}))
    {
        // Do stuff
    }

    return View(model);
}

You can make this even simpler, using BindAttribute

[HttpPost]
public ActionResult New([Bind(Exlude="Password")]RegisterModel model)
{
    if(ModelState.IsValid)
    {
       model.Password = Membership.GeneratePassword(6, 1);
       // Do Stuff
    }

    return View(model);
}

And finally simplest and the best way

Define separate view models

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文