具有强类型 ViewModel 的 ASP.NET MVC xVal

发布于 2024-08-11 08:28:04 字数 756 浏览 8 评论 0原文

我无法使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

享受孤独 2024-08-18 08:28:04

我认为问题在于您不使用前缀。
如果您调试 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.

朕就是辣么酷 2024-08-18 08:28:04

对于 Adrian 来说,我们无法查看您是否在视图模型上使用 DataAnnotations,或者是否使用 本文 。您需要使用类似 DataAnnotationsValidationRunner 的东西,它提到对您使用的验证属性(例如,Required、Range 等)指定的视图模型的每个属性执行验证。

基本上步骤是:

  1. 从表单中检索更新的强类型视图模型。
  2. 将该模型传递给您的 DataAnnotationsValidationRunner,收集导致的任何错误
  3. 如果存在任何错误(ErrorInfo 对象),则将其作为 RulesException 抛出
  4. 捕获 RulesException > 并使用异常的 AddModelStateErrors 方法将验证异常添加到您的模型中
  5. 检查是否 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:

  1. Retrieve your updated strongly-typed view model from the form.
  2. Pass that model to your DataAnnotationsValidationRunner, collecting any errors that result
  3. If there were any errors (ErrorInfo objects), throw them as a RulesException
  4. Catch the RulesException and add the validation exceptions to your model using the exception's AddModelStateErrors method
  5. Check to see if ModelState.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 appropriate ValidationMessage html helper calls in place, also referenced by the linked article.
浊酒尽余欢 2024-08-18 08:28:04

您的帖子不够简洁,不足以让我找出问题所在,但您可以在 此博客文章。本文还描述了您需要一步步执行的所有操作,因此这应该可以帮助您运行 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文