在 .NET 中进行验证时“/”中的服务器错误

Server Error in '/' with Validation in .NET

“/”应用程序中始终存在服务器错误。当我尝试在 Web 应用程序中使用验证检查时。

这是模特:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace MVCDemon.Models
{
    public class Movie
    {
    [DisplayName("MovieName")]
    [Required(AllowEmptyStrings =false, ErrorMessage ="CanNotBeEmpty")]
    public string Name { get; set; }
    }
}

这是控制器:

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

namespace MVCDemon.Controllers
{
    public class MovieController : Controller
    {
        [HttpPost]
        public ActionResult Validation(Movie movie)
        {
            if (ModelState.IsValid)
            {
                ViewBag.Name = movie.Name;
            }
            return View();
        }
    }
}

这是Html:

<div>
@model MVCDemon.Models.Movie
@using (Html.BeginForm())
{
    <div>
        @Html.TextBoxFor(m => m.Name)
        @Html.ValidationMessageFor(m => m.Name)
    </div>
    <input type="submit" value="submit" />
}
</div>
<div>
    @ViewBag.Name
</div>

这是布局:

我检查了脚本并导入了 jquery.validation.min.js 和 jquery.validation.unobtrusive.min.js

此外,我尝试按照有人提到的方式设置配置:

但是错误依然存在

Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

您的表单似乎没有路由到控制器。

您可以通过将以下参数添加到 Html.BeginForm:

@using (Html.BeginForm("Validation", "Movie", FormMethod.Post))
{
    // ...rest of form code
}

重要的参数是actionName, controllerName.