MVC中的验证后Knockoutjs丢失 [英] MVC validation lost in Knockoutjs post

查看:227
本文介绍了MVC中的验证后Knockoutjs丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC4和淘汰赛。我有一个强类型到视图模型在页面上的形式。在这个视图模型我有一些验证定义,例如:

I'm using MVC4 and knockout. I have a form on a page that is strongly typed to a viewmodel. In that viewmodel I have some validation defined, for instance:

[Required(ErrorMessage = "Title is required")]
public string Title { get; set; }

如果我做一个标准的岗位不填充必填字段的形式,我的行动看,该模型是无效的,并返回到视图,并显示一个主验证消息,因为我有 @ Html.ValidationSummary 在我的形式。单个字段也被标记为无效(一条消息),因为我有 @ Html.ValidationMessageFor 与字段关联

If I do a standard post to the form without the required field filled, my action sees that the model is not valid and returns to the view, and a main validation message is displayed because I have @Html.ValidationSummary in my form. The individual field is also marked as invalid (with a message) because I have @Html.ValidationMessageFor associated with the field

于是我加淘汰赛和,而不是仅仅张贴的形式我现在请 ko.utils.postJson($(形式)[0],个体经营); 。现在大部分一切正常,如果我发布形式不填充必填字段,我的动作检测,该模型是无效的,并返回到视图,除了现在的个人验证信息显示不出来。

So then I added knockout and instead of just posting the form I now call ko.utils.postJson($("form")[0], self);. Now most everything works, if I post the form with the required field not populated, my action detects that the model is invalid and returns to the view, except now the individual validation messages don't show up.

在字段中的前填写后也会丢失。我试图通过阅读MVC中的视图模型和像这样的变量设置这些值来解决这个问题。

The filled in fields before the post are also lost. I tried to remedy this by reading in the mvc viewmodel and setting the variables to those values like so.

var model = @Html.Raw(Json.Encode(Model));

然后在我的淘汰赛视图模型设置这个值

And then in my knockout viewmodel setting that value

self.Title = ko.observable(model.Title);

但后来当我进入'1',它会在现场填写1当失败后返回。

But then when I entered '1', it would fill in the field with '"1"' when the failed post returned.

这是不是太复杂的一种形式,所以我不希望引入一些新的验证层,如果能避免它。我不介意学生要验证一个职位。

This is not too complex of a form, so I do not want to introduce some new validation layer if it can be avoided. I don't mind there being a post for the validation.

谢谢,丹

推荐答案

我的问题是什么。我是探索过去2周这个话题。 (如果你想看看从过去的几天我的问题)。我来的结论,即数据的注释和流利的验证不会对正在使用基因敲除的客户端的工作很遗憾。因此,你的必须的介绍验证另一层。

What I question. I was exploring this topic for the last 2 weeks. (Look up my questions from last few days if you want to). I came to conclusion that data annotations and fluent validation doesn't work on client side that is using knockout unfortunately. Hence you have to introduce another layer of validation.

好消息是,其精心测试和标准的淘汰赛是。

Good news is that its well tested and as standard as knockout is.

您主要有三种选择:

  • Knockout Validation
  • Knock Knock
  • jQuery Validation

我期待社会倾向于使用jQuery一个建议,但显然淘汰赛主流验证是首选那里。它的行为非常相似的数据注解,但显然是在淘汰赛视图模型领域做了。

I was expecting community to tend to suggest using jQuery one, but apparently mainstream validation for knockout is the first choice there. It behaves very similarly to data annotations, but is obviously done on knockout view model fields.

希望这有助于(不拍的使者:))

Hope this helps (don't shoot the messenger :) )

这篇关于MVC中的验证后Knockoutjs丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆