没有 ValidationSummary 的模型验证?

发布于 2024-12-10 15:24:42 字数 428 浏览 1 评论 0原文

我的登录操作有一个 LoginModel,但我只想使用 HTML。

示例...

public class LoginModel
{
  [Required]
  public string Email { get;set; }
}

in my HTML, I have
<input type="text" value="" name="Email">

这是因为我要将 HTML 存储在数据库中,我遇到的问题是,如何在不使用 Html.ValidationSummary() 的情况下获得模型验证?

我希望我能做到

<div class="validation-summary-errors"></div>

因为这是 HTML 中的内容,但不起作用..

有想法吗?

I have a LoginModel for my Login Action, but I'm wanting to use just HTML.

Example...

public class LoginModel
{
  [Required]
  public string Email { get;set; }
}

in my HTML, I have
<input type="text" value="" name="Email">

This is because I'm going to be storing my HTML in my database, problem I'm having is, how do I get model validation without using Html.ValidationSummary()?

I was hoping I could just do

<div class="validation-summary-errors"></div>

As this is what is in the HTML, but does not work..

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

亽野灬性zι浪 2024-12-17 15:24:42

无论您将 HTML 存储在何处,验证都是在客户端完成的。有各种关于如何使用虚拟路径提供程序将视图存储在其他地方(DB)的帖子,然后验证应该仍然可以正常工作。我想我不明白为什么它不适合你,所以我必须想象你没有使用路径提供程序来查找你的视图。

编辑

似乎您想将消息注入 Div 中。除非您在路径提供程序中使用一些魔法,否则这不会自动发生。在视图中使用您自己的帮助器方法以避免黑客攻击或仅使用默认提供的方法。如果您确实想要这样做,请在您的 controlllet 中渲染您的视图并搜索要替换的 Div 模式。
自定义 ValidationForMessage 帮助程序删除 css 元素

注意 Darin 的方法

var expression = ExpressionHelper.GetExpressionText(ex);
    var modelName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expression);
    var modelState = htmlHelper.ViewData.ModelState[modelName];

无法访问ViewContext 在你的控制器中你只能渲染你的视图的 html。但是,在您看来,您需要(据我所知)一个辅助方法将错误集合粘贴到 ViewData 中。

您的虚拟路径提供程序可能必须将此辅助方法注入到您的视图文本中,以便 Razor 进行解析。事实上 - 呃。 这可能要容易得多。您的提供商可能只需从数据库中读取您的 html,找到 div,然后将 @Html.ValidationSummary 注入该 div 中。我相信这会起作用。如果验证摘要最终还是会在那里(本质上),为什么不直接把验证摘要放在那里呢?

Regardless of where you store your HTML the validation is done on the client side. There are various posts on how to use the virtual path provider to store your views somewhere else (DB) and then validation should still work fine. I think I'm missing why it's not working for you though so I have to imagine you aren't using the path provider to find your views.

Edit

Seems you want to inject messages into a Div. This wont happen automaticaly unless you work some magic in the path provider. Use your own helper method in the view to avoid hacks or just use what's provided by default. If you really want to do it render your view in your controlllet and search for your Div pattern to replace.
custom ValidationForMessage helper removing css element

Note Darin's method

var expression = ExpressionHelper.GetExpressionText(ex);
    var modelName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expression);
    var modelState = htmlHelper.ViewData.ModelState[modelName];

without access to ViewContext in your controller you can only render your html for your View. However, somewhere in your view you need (as far as I can tell) a helper method to stick your error collection into ViewData.

Your Virtual Path Provider may have to inject this helper method into your view text so it is there for Razor to parse. Actually - duh. This may be much easier. Your provider may be able to just simply read your html from the database, find the div, and inject the @Html.ValidationSummary into that div. I believe this would work. Why not just put the validation summary in there though if its going to end up there in the end anyways (essentially)

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