验证不正常。 ASP.Net Razor(我认为)

时间:2013-11-04 11:38:46

标签: asp.net razor webmatrix

我以前曾经常常使用网站来帮助编程,但是从来没有问过一个问题,如果我做错了,请与我一起讨论:)。

好的,我感到非常困惑 我正在使用WebMatrix,asp.net和razor。遵循这些教程。 http://www.asp.net/web-pages/tutorials/introducing-aspnet-web-pages-2

此页面必须将值插入数据库,它首先完美地完成它,但是当我添加验证时,它会给我这个错误“异常详细信息:System.OverflowException:对于Int32,值太大或太小。 “使用的数据库都是nvarchar,除了作为int的Techid。但我不明白的是当我发表评论时

Validation.Add("no", Validator.Range(9,10 ,"Contact number can not be longer or shorter than 10 digits"));
Validation.Add("num", Validator.Range(13,13,"Is number must be 13 digits"));
Validation.Add("name", Validator.Regex("^[a-zA-Z ]+$", "Invalid format for a name"))

它运作得很好。

@{  
    var db = Database.Open("ProjectTest");
    var Name = Request.Form["name"];
    var Surname = Request.Form["surname"];
    var IDNumber = Request.Form["num"];
    var Address = Request.Form["add"];
    var ContactNumber = Request.Form["no"];
    var Course = Request.Form["course"];
    var TechID = Request.Form["techID"];
    var grade = Request.Form["grade"];

    Validation.RequireField("name", "Name is needed");
    Validation.RequireField("surname", "Surname is needed");
    Validation.RequireField("num", "num is needed");
    Validation.RequireField("add", "add is needed");
    Validation.RequireField("no", "no is needed");
    Validation.RequireField("course", "course is needed");
    Validation.RequireField("grade", "grade is needed");

    Validation.Add("no", Validator.Range(9,10 ,"Contact number can not be longer or shoter than 10 digits"));
    Validation.Add("num", Validator.Range(13,13,"Is number must be 13 digits"));
    Validation.Add("name", Validator.Regex("^[a-zA-Z ]+$", "Invalid format for a name"));  

    if (IsPost)
    {
        if (Validation.IsValid())
        {
            var insertQuery = "INSERT INTO UserInfo (Name, Surname, IdNum, Address, ContNum, Course, Grade) VALUES (@0, @1, @2, @3 ,@4, @5, @6)";
            db.Execute( insertQuery, Name, Surname, IDNumber, Address, ContactNumber, Course, grade );
        }
    }      
    else
    {
          ModelState.AddFormError("There are some errors with your submission"); 
    }  
}


<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <a href="/Home">Home</a><br>
              If you are an Student that wants to register use this form.<br>
       <form method="post">
        <table>
            <tr>
            <td>Name:</td><td><input type="text" name="name"></td>
            </tr>
            <tr>
            <td>Surname:</td><td><input type="text" name="surname"></td>
            </tr>
            <tr>
            <td>ID Number:</td><td><input type="text" name="num"><br></td>              
            </tr>
            <tr>
            <td>Address:</td><td><input type="text" name="add"><br></td>
            </tr>
            <tr>
            <td>Contact Number:</td><td><input type="text" name="no"><br></td>
            </tr>
            <tr>
            <td>Course:</td><td><input type="text" name="course" value=""></td>
            </tr>
            <tr>
            <td>Grade:</td><td><input type="text" name="grade"></td>
            </tr>
            <tr><td><input type="submit" value="Submit"></td>
            </tr>
        </table>
           @Html.ValidationMessage("name")<br>
           @Html.ValidationMessage("surname")<br>
           @Html.ValidationMessage("num")<br>
           @Html.ValidationMessage("add")<br>
           @Html.ValidationMessage("no")<br>
           @Html.ValidationMessage("course")<br>
           @Html.ValidationMessage("grade")
       </form>  
       <select name="courses">
           <option value="1" id="test">S7</option>
           <option value="2">Commerce</option>
       </select>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

Validator.Range将验证数值是否介于最小值和最大值之间。看起来您想要验证输入的长度,因此您应该使用Validator.StringLength()