手动验证文本框使用jQuery不显眼的审定asp.net MVC3 [英] Manually validate textbox with jQuery unobtrusive validation asp.net MVC3

查看:113
本文介绍了手动验证文本框使用jQuery不显眼的审定asp.net MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个多步骤的过程,允许用户找回遗忘的密码。这些步骤是像这样,与在我的控制器的每一步不同的动作:

So I have a multi-step process that allows a user to retrieve a forgotten password. The steps are like so, with a different action for each step in my controller:


  1. 输入用户名和电子邮件

  2. 输入密码提示问题的答案

  3. 电子邮件恢复链接,用户如果一切顺利

我试图用一种型号的一切:

I tried using one model for everything:

public class AccountForgotPassword
{

    [Required()]
    [DisplayName("Username")]
    public string UserName { get; set; }

    [Required()]
    public string Email { get; set; }

    [Required()]
    public string PasswordAnswer { get; set; }

}

但是,当我检查ModelState.IsValid上的第一个动作,因为用户不能输入密码答题的是,它永远是假的,使得一些有趣的code,检查模型状态是其实有效的,但唯一缺少的密码答案,因为我不知道用户是谁呢。

But when I check ModelState.IsValid on the first action, since the user can't input their password answer question yet, it will always be false and makes for some interesting code to check that the model state is in fact valid but is only missing the password answer since I don't know who the user is yet.

要解决这个问题,我决定放弃类型化模式,只是在我的动作中使用字符串参数。现在唯一的问题是,我可以不再使用容易验证线起坐您模型绑定搞定。

To get around this, I decided to forgo a typed model and just use string parameters in my actions. Only problem now is that I can no longer use the easy validation wire-ups you get with model binding.

随着中说,没有人知道一个简单的方法来手动线了jQuery验证单个输入,因此它会检查所需的规则?此外,将这种接线允许我使用验证器生成默认的错误信息,否则我将不得不在它们连接了我自己的供应?难道是这很容易在我看来:

With that said, does anyone know an easy way to manually wire-up the jQuery validation to individual inputs so it will check the required rule? Also, will this wiring allow me to use the default error messages the validator generates, or will I have to supply my own upon wiring them up? Would it be this easy in my view:

@{
    ViewBag.Title = "Forgot Password";
}
<h2>
    Forgot Password</h2>
@using (Html.BeginForm())
{ 
    <p>
        @Html.Label("UserName", "Username")
        @Html.TextBox("UserName")
        @Html.ValidationMessage("UserName")
    </p>

    <p>
        @Html.Label("Email", "Email")
        @Html.TextBox("Email")
        @Html.ValidationMessage("Email")
    </p>

    <p>
        <input type="submit" value="Send Form" />
    </p>
}

<script type="text/javascript">
    $.validator.unobtrusive.addRule(..something here...);
</script>

此外,如果有更好的方式来做到这一点,请让我知道。先谢谢了。

Also if there is a better way to do this, please let me know. Thanks in advance.

更新

有关其他任何人发现这一点,我没弄清楚如何手动添加规则。如果刚才所读验证文档第一。假设上查看HTML,脚本将是:

For anyone else that finds this, I did figure out how to add the rules manually. Should have just read the validation docs first. Assuming the view html above, the script would be:

<script type="text/javascript">
    $(function () {
        $('#UserName').rules('add', {
            required: true,
            messages: {
                required: 'The username field is required.'
            }
        });

        $('#Email').rules('add', {
            required: true,
            messages: {
                required: 'The email field is required.'
            }
        });

        $.validator.unobtrusvie.parse('form');
    });
</script>

不过,想着它多一些,并采取AFinkelstein的回答进去后,我想我会先走一步,让2个不同的视图模型,让框架做的工作对我来说。

However, after thinking about it some more and taking AFinkelstein's answer into account, I think I will just go ahead and make 2 different view models and let the framework do the work for me.

推荐答案

如果你只是验证用户名和密码,然后再密码回答第二个,我觉得最简单的解决方案是让两个独立的视图模型。然后,你仍然可以使用每个部分的相应视图模型和验证。

If you validate just the username and password first, and then the Password answer second, I think the easiest solution is to have two separate View Models. Then you can still use the appropriate View Model and validation for each section.

这篇关于手动验证文本框使用jQuery不显眼的审定asp.net MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆