具有强类型 ViewModel 的 ASP.NET MVC xVal
我无法使用 xVal 验证 来处理强类型视图模型。
xVal 中的每个方法似乎都需要一个在处理强类型视图模型时不使用的前缀。
我的视图包含与此类似的代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ContactForm>" %>
<%= Html.TextBox("firstName", Model.FirstName) %>
并且以下代码进入控制器:
try
{
var theModel = form.ToModel();
_contactRepository.Save(theModel);
}
catch (RulesException ex)
{
ex.AddModelStateErrors(ModelState, string.Empty); // Passing string.Empty for prefix, since I don't use prefixes.
}
return View(form);
但是,上面的代码不起作用。我肯定错过了一些东西,但不知道是什么。这是我第一次使用 xVal。
感谢您的帮助!
I can't get xVal validation to work with strongly typed viewmodels.
Every method in xVal seems to want a prefix which is not used when dealing with strongly typed viewmodels.
My view contains code similar to this:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ContactForm>" %>
<%= Html.TextBox("firstName", Model.FirstName) %>
And the following code goes in the controller:
try
{
var theModel = form.ToModel();
_contactRepository.Save(theModel);
}
catch (RulesException ex)
{
ex.AddModelStateErrors(ModelState, string.Empty); // Passing string.Empty for prefix, since I don't use prefixes.
}
return View(form);
However, the above code doesn't work. I've surely missed something, but don't know what. This is my first time using xVal.
Thankful for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为问题在于您不使用前缀。
如果您调试 ModelState,您可以看到诸如“.FirstName”之类的验证,尽管它们应该类似于“FirstName”。因此,客户端验证摘要和内容不会显示这些验证错误消息。
我认为这是 xVal 中的一个错误。
I think the problem is in the fact that you don't use prefixes.
If you debug the ModelState you can see validations for things like ".FirstName" although they should be like "FirstName". Because of that the client side validation summaries and stuff doesn't show those validation error messages.
I think this is a bug in xVal.
对于 Adrian 来说,我们无法查看您是否在视图模型上使用 DataAnnotations,或者是否使用 本文 。您需要使用类似 DataAnnotationsValidationRunner 的东西,它提到对您使用的验证属性(例如,Required、Range 等)指定的视图模型的每个属性执行验证。
基本上步骤是:
DataAnnotationsValidationRunner
,收集导致的任何错误RulesException
抛出RulesException
> 并使用异常的AddModelStateErrors
方法将验证异常添加到您的模型中ModelState.IsValid
,如果不是,则表示您的视图,这要归功于您的异常处理现在会将错误绑定到您的视图模型。您必须确保有适当的ValidationMessage
html 帮助器调用,链接的文章也引用了该调用。To Adrian's point, we can't see if you are using DataAnnotations on your view model, or if you are using any sort of runner described in this article . You'll need to use a something like the DataAnnotationsValidationRunner it mentions to execute validation on each property of your view model as specified by the validation attributes you use (e.g., Required, Range, etc.).
Basically the steps would be:
DataAnnotationsValidationRunner
, collecting any errors that resultRulesException
RulesException
and add the validation exceptions to your model using the exception'sAddModelStateErrors
methodModelState.IsValid
, and if it isn't, represent your view, which thanks to your exception handling will now have the errors bound to your view model. You'll have to make sure you have the appropriateValidationMessage
html helper calls in place, also referenced by the linked article.您的帖子不够简洁,不足以让我找出问题所在,但您可以在 此博客文章。本文还描述了您需要一步步执行的所有操作,因此这应该可以帮助您运行 xVal。
Your posting to not nearly concise enough for me to figure out what's going wrong, but you can find a fully working demo website at the end of this blog article. The article also describes everything you need to do step by step, so this should help you get xVal running.