MVC5 视图模型的验证未到达控制器
Validation of the MVC5 View Model does not reach the controller
我有一个 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; }
}
控制器:
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>
}
我不明白发生了什么。我在其他几个部分都有相同的代码,它们工作得很好。
我找遍了我所有的东西,我无法解决...
有人可以帮助解决这个问题吗?
此外,验证消息显示在屏幕上。但是我无法访问我的控制器,因为它发生在其他屏幕上
不要使用 jQuery 验证器。它将检查验证错误,并且在验证失败的情况下不会让请求到达您的控制器。既然你说客户端验证正在进行,我只能猜测是这种情况。禁用 jQuery 验证器,即使您的视图模型无效,请求也会到达控制器。
如果您使用了 mvc 5 项目的默认模板,请在 bundle.config 文件中查找它。在那里你可以评论它。
客户端验证似乎在您不希望的时候妨碍了您。默认情况下,如果您使用 MVC 样板代码,它将自动设置并打开表单的客户端验证。这将在 javascript 中验证客户端的必填字段之类的内容,并在未通过此客户端验证时阻止表单发布到服务器。
您可以在此处阅读它的工作原理:https://www.blinkingcaret.com/2016/03/23/manually-use-mvc-client-side-validation/(本文讲述了如何手动使用它,但很好地解释了它的工作原理)
但是如果你想在你的控制器中处理所有服务器端验证,你可以通过几种方式禁用客户端验证:
在您的包配置中删除对 jquery.validate.js
和 jquery.validate.unobtrusive.js
的脚本引用
将 <add key="ClientValidationEnabled" value="false"/>
和 <add key="UnobtrusiveJavaScriptEnabled" value="false"/>
添加到 <appSettings>
节点下的 weeb.config
- 将
HtmlHelper.ClientValidationEnabled = false;
和 HtmlHelper.UnobtrusiveJavaScriptEnabled = false;
添加到个人视图或操作中
我有一个 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; }
}
控制器:
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>
}
我不明白发生了什么。我在其他几个部分都有相同的代码,它们工作得很好。
我找遍了我所有的东西,我无法解决...
有人可以帮助解决这个问题吗?
此外,验证消息显示在屏幕上。但是我无法访问我的控制器,因为它发生在其他屏幕上
不要使用 jQuery 验证器。它将检查验证错误,并且在验证失败的情况下不会让请求到达您的控制器。既然你说客户端验证正在进行,我只能猜测是这种情况。禁用 jQuery 验证器,即使您的视图模型无效,请求也会到达控制器。
如果您使用了 mvc 5 项目的默认模板,请在 bundle.config 文件中查找它。在那里你可以评论它。
客户端验证似乎在您不希望的时候妨碍了您。默认情况下,如果您使用 MVC 样板代码,它将自动设置并打开表单的客户端验证。这将在 javascript 中验证客户端的必填字段之类的内容,并在未通过此客户端验证时阻止表单发布到服务器。
您可以在此处阅读它的工作原理:https://www.blinkingcaret.com/2016/03/23/manually-use-mvc-client-side-validation/(本文讲述了如何手动使用它,但很好地解释了它的工作原理)
但是如果你想在你的控制器中处理所有服务器端验证,你可以通过几种方式禁用客户端验证:
在您的包配置中删除对
jquery.validate.js
和jquery.validate.unobtrusive.js
的脚本引用将
<add key="ClientValidationEnabled" value="false"/>
和<add key="UnobtrusiveJavaScriptEnabled" value="false"/>
添加到<appSettings>
节点下的 weeb.config- 将
HtmlHelper.ClientValidationEnabled = false;
和HtmlHelper.UnobtrusiveJavaScriptEnabled = false;
添加到个人视图或操作中