简体   繁体   English

asp.net mvc 3 silverlight像验证

[英]asp.net mvc 3 silverlight like validation

MAIN QUESTION 主要问题

As the title says, i'm asking if anyone knows how to create a validation structure similar to silverlight's dataform. 如标题所述,我要问是否有人知道如何创建类似于silverlight数据格式的验证结构。 Any hints, materials , ideas would be welcome. 任何提示,材料,想法都将受到欢迎。 I'd like to do a validation like this: 我想做这样的验证:

在此处输入图片说明

(You can check this out here ) (您可以在这里查看

DETAILS 细节

I've been trying to something like the example above, but without results until now. 我一直在尝试类似上面的示例,但是到目前为止没有结果。 Mostly because i dont' know how the validation message helper works. 主要是因为我不知道验证消息助手的工作原理。 I've managed to a single validation message by taking the data-val-number attribute and set it into a link title (using a third party jquery plugin callled qTip to show a tooltip error message). 我已经通过采用data-val-number属性并将其设置为链接标题(使用名为jTip的第三方jquery插件显示工具提示错误消息)来处理一条验证消息。 However, i can't do the same thing if there are more than one validation. 但是,如果有多个验证,则我无法做同样的事情。

So, is it possible to rewrite the validation message helper? 因此,可以重写验证消息助手吗? I'd like to understand more how it shows the validation messages so i can put them on any html content. 我想了解更多它如何显示验证消息,以便可以将它们放在任何html内容上。 This would do the tooltip part and i could set as many messages as i want, with any formatting i desire. 这将完成工具提示部分,并且我可以设置任意数量的消息,并且可以设置任何格式。

And i'd like to be able to show the validation messages through any jquery events (mouseover, click, dblclick, ready, etc.). 而且我希望能够通过任何jquery事件(鼠标悬停,单击,dblclick,就绪等)显示验证消息。 As far as i understand on it's actual implementation, the validation only occurs when the user changes focus from the actual input to another html element. 据我对它的实际实现了解,仅当用户将焦点从实际输入更改为另一个html元素时,才会进行验证。

I highly recommend that you checkout the validation rules section in Professional ASP.NET MVC . 我强烈建议您检出Professional ASP.NET MVC中的验证规则部分。 It shows how to do exactly what you describe with ASP.NET MVC v1 and it works all the way up through v3. 它显示了如何完全使用ASP.NET MVC v1进行描述,并且在v3之前一直有效。

The displayed user interface is slightly different; 显示的用户界面略有不同; however, you can check the output and write your own CSS to make it look just like your screenshot. 但是,您可以检查输出并编写自己的CSS,使其看起来就像您的屏幕截图一样。

For a quick example: 举个简单的例子:

Action Result 行动结果

try 
{
    // code
}
catch
{
    foreach (var issue in std.GetRuleViolations())
    {
        ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
    }
}

Model 模型

public IEnumerable<RuleViolation> GetRuleViolations()
{
    if (!String.IsNullOrEmpty(this.Phone) && !Utility.IsValidPhoneNumber(this.Phone))
    {
        yield return new RuleViolation("Phone is invalid. Try this format: ###-###-####.", "HomePhone");
    }  

    yield break;         
}

Edit View 编辑视图

<% 
    using (Html.BeginForm()) 
    {
%>
<fieldset>
    <legend>Edit</legend>
    <%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
    <%= Html.TextBox("Phone", Model.Phone)%>
    <%= Html.ValidationMessage("Phone", "*")%>
    <!-- more fields go here -->
</fieldset>
<% } %>

rule violation class -- lifted from link 违反规则类-从链接中取消

public class RuleViolation
{
    public string ErrorMessage { get; private set; }
    public string PropertyName { get; private set; }

    public RuleViolation(string errorMessage)
    {
        ErrorMessage = errorMessage;
    }

    public RuleViolation(string errorMessage, string propertyName)
    {
        ErrorMessage = errorMessage;
        PropertyName = propertyName;
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM