Asp.net MVC5视图模型的验证未到达控制器

Asp.net MVC5视图模型的验证未到达控制器,asp.net,.net,asp.net-mvc-5,Asp.net,.net,Asp.net Mvc 5,我有一个Asp.Net MVC5项目,当在ViewModel上使用requires属性时,如果ViewModel无效,我将无法访问控制器。但是,它只发生在特定的屏幕上 我需要的是,即使我的VM错误,这个请求也会到达我的控制器,以便在我的视图中发生另一个操作(在本例中,一个微调器被隐藏) 我的代码示例如下: 视图模型: public class ParameterizationViewModel { /// <summary> /// Name of

我有一个Asp.Net MVC5项目,当在ViewModel上使用requires属性时,如果ViewModel无效,我将无法访问控制器。但是,它只发生在特定的屏幕上

我需要的是,即使我的VM错误,这个请求也会到达我的控制器,以便在我的视图中发生另一个操作(在本例中,一个微调器被隐藏)

我的代码示例如下:

视图模型:

public class ParameterizationViewModel 
{

        /// <summary>
        /// Name of Parameterization.
        /// </summary>

        [Required(ErrorMessageResourceName = "LabelErrorFieldRequired", ErrorMessageResourceType = typeof(ResourcesGSC.Language))]
        [Display(Name = "LabelName", ResourceType = typeof(ResourcesGSC.Language))]
        public string Name { get; set; }
} 
视图:

@model Project.Models.Parameterization.ParameterizationViewModel
@{
ViewBag.Title=resourcesgssc.Language.LabelParameterizationMenu;
}
@使用(Html.BeginForm(“,”参数化“,”FormMethod.Post,new{}))
{
@LabelFor(m=>m.Name,新{})
@TextBoxFor(m=>m.Name,新的{@class=“form control”,placeholder=”“})
@Html.ValidationMessageFor(m=>m.Name,“,new{@class=“text danger”})
@ResourcesGSC.Language.LabelBtnSave
}
我不明白发生了什么。我在其他几个部分有相同的代码,它们工作得很好

我找遍了所有我得到的东西,但我无法解决它

有人能在这个问题上提供帮助吗


此外,验证消息将显示在屏幕上。但我无法访问我的控制器,因为它发生在其他屏幕上

不要使用jQuery验证程序。它将检查验证错误,如果验证失败,它不会让请求到达控制器。既然你说客户端验证正在进行,我只能猜测情况就是这样。禁用jQuery验证程序,即使您的视图模型无效,请求也会到达控制器


如果您使用了MVC5项目的默认模板,请在bundle.config文件中查找它。在那里你可以评论出来

当您不希望客户端验证时,它似乎正在妨碍您。默认情况下,如果使用MVC样板代码,它将自动设置并启用表单的客户端验证。这将在javascript中验证客户端上的必填字段等内容,并防止表单在未通过此客户端验证时发布到服务器

您可以在这里阅读它的工作原理:(本文介绍了如何手动使用它,但很好地解释了它的工作原理)

但是,如果要在控制器中处理所有验证服务器端,可以通过以下几种方式禁用客户端验证:

  • 删除bundle配置中对
    jquery.validate.js
    jquery.validate.unobtrusive.js
    的脚本引用

  • 添加到
    节点下的weeb.config中

  • 添加
    HtmlHelper.ClientValidationEnabled=false
    htmlhelp.UnobtrusiveJavaScriptEnabled=false到单个视图或操作

那么有效的虚拟机是否会进入您的控制器?这是您不想要的客户端验证吗?是的,如果我的VM有效,它将到达我的控制器。我不知道如何向你解释清楚。客户端验证发生,但我需要它首先到达我的控制器。我想我可以说我不想要像快速失效验证这样的东西,我认为这就是正在发生的事情。。。我不知道这对您是否有意义,但如果您感到困惑,我可以尝试更好地解释它到您的视图(在
视图包.Title
部分下)并查看它是否得到您期望的结果?它正在工作!!Thx zgood!!!你能解释一下为什么吗?在我的项目中,我有很多其他的视图,所以我不必这样做,并且按照预期工作。只有这个观点让我陷入麻烦…错过了3分钟…哈哈。谢谢桑杰!!你的回答也是正确的。然而,zgood补充了一个更完整的答案,可以在未来帮助更多的人。无论如何谢谢你!
public class ParameterizationController : BaseController
    {
        [HttpGet]
        public ActionResult Index(string id)
        {
            var model = new ParameterizationViewModel();

            if (String.IsNullOrEmpty(id))
            {
                //Code omitted
                //Here, I structure my model to be a clean view
            }
            else
            {
                //Code omitted
                //Here, I structure my model to be a screen filled with recovered data
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(ParameterizationViewModel model)
        {
            if (!ModelState.IsValid)
            {
                //Here, I validate my ViewModel. I need you to get here, but it doesn't. 
                return View(model);
            }
            //Code omitted
            //Here, follow the flow of persistence with WebService
        }
    }
@model Project.Models.Parameterization.ParameterizationViewModel

@{
    ViewBag.Title = ResourcesGSC.Language.LabelParameterizationMenu;
}


@using (Html.BeginForm("", "Parameterization", FormMethod.Post, new { }))
{
    <div class="form-group">
        <div class="row mb-3">
            <div class="col-lg-6 col-md-12">
                @Html.LabelFor(m => m.Name, new { })
                @Html.TextBoxFor(m => m.Name, new { @class = "form-control", placeholder = "" })
                @Html.ValidationMessageFor(m => m.Name, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>

    <div class="row mb-3">
        <div class="col">
            <button type="submit" class="btn btn-primary float-right">
                @ResourcesGSC.Language.LabelBtnSave
            </button>
        </div>
    </div>
}