在 .NET MVC2 中使用 Knockoutjs 和服务器端验证

发布于 2024-12-31 23:34:13 字数 497 浏览 0 评论 0原文

我正在使用MVC2。

使用剔除时服务器端表单验证的推荐方法是什么?

目前,我的大多数表单都在部分视图中,其中具有带有验证属性的 C# ViewModel。像这样的事情:

public class SomeThingViewModel
{
    [Required]
    public string Name { get; set; }

    [Required]
    public int Number{ get; set; }
}

因此,当表单提交到服务器时,我会收到所有模型错误,并且可以返回带有错误的表单,这些错误显示为: <%: Html.ValidationMessageFor(m => ; m.Name)%>.然后将其重新加载到主页上保存表单的元素中,以便用户可以看到错误。这将消除我所假设的淘汰赛形式的任何绑定。

我不太确定如何使用淘汰赛来解决这个问题。

I'm using MVC2.

Whats the recommended way of server side validation of forms when using knockout?

Currently, most of my forms are in partial views, which have a C# ViewModel with Validation Attributes. Something like this:

public class SomeThingViewModel
{
    [Required]
    public string Name { get; set; }

    [Required]
    public int Number{ get; set; }
}

So when the form gets submitted to the server, I get all the model errors and I can return the form with errors, which are displayed with something like: <%: Html.ValidationMessageFor(m => m.Name)%>. This is then reloaded into the element that holds the form on the main page so that the user can see the errors. This would kill any bindings I had with the form in knockout I would assume.

I'm not really sure how to go about this using knockout.

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

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

发布评论

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

评论(1

烟花易冷人易散 2025-01-07 23:34:13

这可能很棘手,但如果做得好,效果会轻而易举。

首先,同步您的视图模型。淘汰赛中客户端的内容将准确地传递到服务器。其次,不要在服务器端 HTML 中进行剔除。创建在服务器端设置并在客户端读取的字段,以指示 ViewModel 中每个数据字段的有效性。

因此,如果您的 Model 有一个字段 Name,那么您的 ViewModel 就有 NameName_ValidationResult,后者是一个枚举,指示 Name 字段是否为有效以及为什么无效。如果服务器端验证失败,请设置验证结果字段,并将整个服务器端 ViewModel 传递回客户端,以便在请求完成后重新设置为客户端 ViewModel。基本上,您需要重新创建 ASP.NET 的 ViewState 部分,但是采用可与 Knockout.js 配合使用的格式。

在客户端,您会看到仅根据 ValidationResult 的值显示的错误消息 字段。因此,您可能会看到一条预设的错误消息,指出“必须设置名称字段”,仅当 Name_ValidationResult 的值为“空”(例如)时才会显示该错误消息。

基本上,您实际上使用了 MVVM 模式,并进行了一些细微的调整,以解决必须往返服务器的问题。

因此,您建议我在 C# ViewModel 中为每个属性添加 ValidationResult 字段。然后,当我检查模型的有效性时,在控制器中设置 ValidationResult 属性。然后将视图模型作为 JSON 传回?这样我就可以更新我的淘汰视图模型。这将需要我在某种程度上手动验证,对吧?或者我可以利用最终会出现的 ModelState 错误吗? – 布兰卡龙

对于你所有的问题,底线都是肯定的。

事实上,我错过了您使用 DataAnnotations 进行验证的事实,否则我会提到它。您应该能够利用 ModelState 错误来设置您传递回剔除页面的验证结果。

问题在于,您正在使用两种根本不兼容的技术,并希望它们能够很好地协同工作,但我认为这不会按照您希望的方式进行。必须付出一些代价,我建议最好的办法是服务器端。喝下绝妙的清凉饮料并修复服务器端的问题。

This can be tricky, but done right works like a breeze.

First, synchronize your viewmodels. What you have client-side in knockout you pass exactly to the server. Second, don't do server-side HTML with knockout. Create fields that are set server-side and read client-side that indicate the validity of each data field in your ViewModel.

So if your Model has a field Name, your ViewModel has Name and Name_ValidationResult, which is an enum that indicates whether or not the Name field is valid and why it's not. If your server-side validation fails, set your validation result fields and pass the whole server-side ViewModel back to the client to be re-set as the client-side ViewModel after the request completes. Basically, you a re-creating the ViewState portion of ASP.NET, but doing so in a format that will work with Knockout.js

On the client-side, you have error messages that only show based on values of the ValidationResult fields. So you might have a canned error message that states "The Name field must be set" that is only displayed if Name_ValidationResult has the value "Empty" (for example).

Basically, you actually use the MVVM pattern with a minor tweak to account for having to round-trip to the server.

So you are suggesting that I add ValidationResult fields in my C# ViewModel for each property. Then set the ValidationResult Properties in my controller when I check for the Model's validity. Then pass back the viewmodel as JSON? so that I can update my knockout viewmodel. This will require me to manually validate to some extent right? Or can I leverage the ModelState errors that I will end up with? – Blankasaurus

Bottom line is yes to all your questions.

In truth, I missed the fact that you were using DataAnnotations for your validation, or I'd have mentioned it. You should be able to leverage ModelState errors to set your validation results that you pass back to your knockout page.

The problem is that you're using two fundamentally incompatible technologies and hoping they'll play nice together, and I don't think that's going to work out the way you hope. Something is going to have to give, and I suggest that the best point for that is server-side. Drink the knockout cool-aid and fix what you have to server-side.

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