Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.NET MVC中的必需和范围验证不起作用_Asp.net Mvc - Fatal编程技术网

Asp.net mvc ASP.NET MVC中的必需和范围验证不起作用

Asp.net mvc ASP.NET MVC中的必需和范围验证不起作用,asp.net-mvc,Asp.net Mvc,我刚开始学习ASP.NETMVC(不是核心),似乎无法验证模型。我的代码片段如下所示: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace MVCDatabasePractice.Models { public class EmployeeModel {

我刚开始学习ASP.NETMVC(不是核心),似乎无法验证模型。我的代码片段如下所示:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace MVCDatabasePractice.Models
{
    public class EmployeeModel
    {
        [Display(Name = "Employee ID")]
        [Range(100000, 999999)]
        public int EmployeeId { get; set; }

        [Display(Name = "First Name")]
        [Required]
        public string FirstName { get; set; }

        [Display(Name = "Last Name")]
        [Required(ErrorMessage = "Enter in last name")]
        public string LastName { get; set; }

        [Display(Name = "Email Address")]
        [Required(ErrorMessage = "Enter in email address")]
        [DataType(DataType.EmailAddress)]
        public string EmailAddress { get; set; }

        [Display(Name = "Confirm Email")]
        [Compare("EmailAddress", ErrorMessage = "Confirm email doesn't match email address")]
        public string ConfirmEmail { get; set; }

        [Display(Name = "Password")]
        [Required(ErrorMessage = "Enter in password")]
        [DataType(DataType.Password)]
        [StringLength(100, MinimumLength = 8, ErrorMessage = "Password length incorrect")]
        public string Password { get; set; }

        [Display(Name = "Confirm Password")]
        [DataType(DataType.Password)]
        [Compare("Password", ErrorMessage = "Confirm password doesn't match password")]
        public string ConfirmPassword { get; set; }

    }
}
[Display(Name=“…”)
部分在最终形式中工作得非常好,但是
[Range]
[Compare]
[Required]
就是不工作。(
[DataType(DataType.Password)]
仍然有效)

向他们传递更多参数,如
ErrorMessage=“此字段是必需的”
也不起作用

我做错了什么?我没有任何错误

对于提问者,这是控制器中的代码,非常基本:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCDatabasePractice.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult SignUp()
        {
            ViewBag.Message = "Employee Sign Up";

            return View();
        }
    }
}
以及视图文件中的代码:

@model MVCDatabasePractice.Models.EmployeeModel

@{
    ViewBag.Title = "Employee Sign Up";
}

<h2>SignUp</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Employee Model</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.EmployeeId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EmployeeId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EmployeeId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EmailAddress, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EmailAddress, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ConfirmEmail, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ConfirmEmail, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ConfirmEmail, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
@model MVCDatabasePractice.Models.EmployeeModel
@{
ViewBag.Title=“员工注册”;
}
报名
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
员工模式

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.EmployeeId,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.EmployeeId,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.EmployeeId,“,new{@class=“text danger”}) @LabelFor(model=>model.FirstName,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.FirstName,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.FirstName,“,new{@class=“text danger”}) @LabelFor(model=>model.LastName,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.LastName,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.LastName,“,new{@class=“text danger”}) @LabelFor(model=>model.EmailAddress,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.EmailAddress,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.EmailAddress,“,new{@class=“text danger”}) @LabelFor(model=>model.ConfirmEmail,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.ConfirmEmail,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.ConfirmEmail,“,new{@class=“text danger”}) @LabelFor(model=>model.Password,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Password,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Password,“,new{@class=“text danger”}) @LabelFor(model=>model.ConfirmPassword,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.ConfirmPassword,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.ConfirmPassword,“,new{@class=“text danger”}) } @ActionLink(“返回列表”、“索引”)
多亏了你,我才解决了这个问题

我反复检查以确保
Scripts
文件夹中有
jquery.validate.min.js
jquery.validate.unobtrusive.min.js
文件,然后使用它们
@section Scripts{@Scripts.Render(“~/bundles/jqueryval”)}
jqueryval
是捆绑包的名称,我在
App\u Start
下的
BundleConfig.cs
文件中选中了该捆绑包) 在我看来,我希望引用jquery包


现在一切都很好。

多亏了你,我才解决了这个问题

我反复检查以确保
Scripts
文件夹中有
jquery.validate.min.js
jquery.validate.unobtrusive.min.js
文件,然后使用它们
@section Scripts{@Scripts.Render(“~/bundles/jqueryval”)}
jqueryval
是捆绑包的名称,我在
App\u Start
下的
BundleConfig.cs
文件中选中了该捆绑包) 在我看来,我希望引用jquery包


现在一切都很好。

你必须提供更多的信息。你为什么认为它不起作用?显示控制器代码,可能是调试器中的断点。仅使用模型是不可能回答的。您是否下载了验证脚本并将其添加到项目-
jquery.validate.min.js
jquery.validate.unobtrusive.min.js
,并在视图中引用了包,如
@section scripts{@scripts.Render(~/bundles/YourjQueryValidateBundle”)
?谢谢!我已经下载了两个提到的文件,我只是从来没有使用
@section Scripts{@Scripts.Render(“~/bundles/jqueryval”)}
来引用包。现在一切都很好。你必须提供更多的信息。你为什么认为它不起作用?显示控制器代码,可能是调试器中的断点。仅使用模型是不可能回答的。您是否下载了验证脚本并将其添加到项目中-
jquery.validate.min.js
jquery.validate.unobtrusive.min.js
,并在视图中引用了包,如
@section scripts{@scripts.Render(“~/bundles/Your”)