验证在MVC三无模型 [英] Validation in MVC 3 without a Model

查看:117
本文介绍了验证在MVC三无模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在MVC3验证的问题。内置的验证看起来不错。不过,我已经在一个案例中使用JavaScript,使其不一致瓦特/外观和感觉(警告窗口VS漂亮的红色文本)。我们有一个包含用户输入几个字段的形式。提交时,一些Ajax code类火灾映射到这需要从表单提交的值,并揭开序幕这导致客户端数据库创建过程的控制方法的链接。
现在的问题是:什么是做上的字段(长度,字符等),因为没有模式可以直接映射到屏幕上的字段验证的最佳方法?
我的解决办法是写一些JavaScript功能,但有一个更清洁的方式做到这一点?

 < TD> @ Html.TextBox(NewClientId)< / TD>
...    < SCRIPT LANGUAGE =JavaScript的>
       功能ValidateFieldLength(最小,最大,元素){
            VAR LEN = element.value.length;
            如果(LEN<分|| LEN>最大)
                返回false;
            其他{
                返回true;
            }
        }
        功能createNewClient(){
            如果(!ValidateFieldLength(3,3-,的document.getElementById('NewClientId'))){
            警报(无效的客户端ID长度);
            返回;
        }
        $阿贾克斯({
            网址:'/ API /客户,
            输入:'把',
            数据:JSON.stringify({
                客户端Id:$('#NewClientId)VAL()。
                名称:$('#NewClientName)VAL()。
            }),
            的contentType:应用/ JSON的;字符集= UTF-8,
            成功:函数(效应初探){
                //alert(reponse.data.model.Id)​​;
                警报(数据库创建);
            },
            错误:功能(错误){
                警报(ERR);
            }
        });
    }


解决方案

另一种选择我看到的是添加的验证数据属性手动HTML元素。通过这种方式,您可以避免重复在服务器和客户端的错误信息和其他属性。

有关前。

  @ Html.TextBox(NoOfJoinees,,新
{
   尺寸= 5,
   data_val_required =没有。joinees的需要,
   data_val_number =joinees的场号必须是数字。
   data_val_range =joinees号应该是​​最小2和不大于10,
   data_val_range_max =10,
   data_val_range_min =2
})

在上面的文本框我已经添加了三种类型的验证:要求 键入范围那么容易被添加的数据属性。该的不引人注目的验证库的微软运会采取其他的事情。

您应该从一个地方读取错误消息和其他常量。所以你不需要复制它们,当你在服务器端做验证。

I have a question on validation in mvc3. The built in validation looks great. However, I've had to use javascript in one case, causing it to be inconsistent w/look and feel (alert window vs nice red text). We have a form which contains a few fields for user input. When submitted, some ajax code fires a link which maps to a controller method that takes the values submitted from the form and kicks off processes which result in a client database being created. The question is: What is the best way to do validation on the fields (length, character, etc) since there is no model directly mapped to the fields on that form? My solution was to write some javascript functions but is there a cleaner way to do it?

 <td>@Html.TextBox("NewClientId")</td>
...            

    <script language="javascript">
       function ValidateFieldLength(min, max, element) {
            var len = element.value.length;
            if (len < min || len > max)
                return false;
            else {
                return true;
            }
        }
        function createNewClient() {
            if (!ValidateFieldLength(3,3,document.getElementById('NewClientId'))) {
            alert("Invalid Client ID length");
            return;
        }
        $.ajax({
            url: '/api/Clients',
            type: 'PUT',
            data: JSON.stringify({
                ClientId: $('#NewClientId').val(),
                Name: $('#NewClientName').val()
            }),
            contentType: 'application/json; charset=utf-8',
            success: function (reponse) {
                //alert(reponse.data.model.Id);
                alert("Database created");
            },
            error: function (err) {
                alert(err);
            }
        });
    }

解决方案

The other option I would see is adding the validation data attributes manually to the html element. By this way you can avoid duplicating the error messages and other properties in both server and client side.

For ex.

@Html.TextBox("NoOfJoinees", "", new 
{ 
   size = 5,  
   data_val_required="No. of joinees is required",
   data_val_number = "The field No. of joinees must be a number.",
   data_val_range = "No. of joinees should be minimum 2 and not more than 10",
   data_val_range_max="10",
   data_val_range_min="2"
})

In the above textbox I've added three types of validations: required, type and range so easily by adding the data attributes. The unobtrusive validation library shipped by microsoft will take care of the rest.

You should read the error messages and other constants from a single place. So you don't need to replicate them when you are doing the validation at the server-side.

这篇关于验证在MVC三无模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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