我怎么ModelState.AddModel错误绑定到淘汰赛动态创建的验证跨度? [英] How do I get ModelState.AddModel Error to bind to knockout dynamically created validation spans?

查看:106
本文介绍了我怎么ModelState.AddModel错误绑定到淘汰赛动态创建的验证跨度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置Knockoutjs动态创建使用以下code值的可编辑列表:

I have setup Knockoutjs to dynamically create an editable list of values using the following code:

       var requirementModel = function() {
        var self = this;
        self.requirementtypes = ko.observableArray(@Html.Interpret(Model.requirementtypes));
        self.requirementid = ko.observable(@Html.Interpret(Model.requirementid));
        self.AddRequirementType = function() {
            self.requirementtypes.push({
                requirementtypeid: null,
                number: "",
                requirementid: 0
            });
        };
        self.RemoveType = function(Type) {
            self.requirementtypes.remove(Type);
        };
        self.hookUpValidation = function() {
            $.validator.unobtrusive.parseDynamicContent('.dynamicData');
        };
    };
    var viewModel = new requirementModel();
    ko.applyBindings(viewModel);

使用HTML:

 <div class="small-box dynamicData"  data-bind="template:{ name: 'requirementType-template', foreach: requirementtypes, afterRender:$root.hookUpValidation }" ></div>
<button data-bind='click: AddType'>Add Type</button>

我已经迷上了验证使用code推荐的<动态数据href=\"http://stackoverflow.com/questions/4406291/jquery-validate-unobtrusive-not-working-with-dynamic-injected-elements\">stackoverflow.

当我回发到服务器(我没有使用JSON刚刚形成后)我可以做更复杂的验证,如果失败的东西我可以用ModelState.AddModelError(输入字段名,我可怜的傻瓜了打破了这种);此次与完美的作品无论是强类型或@ Html.ValidationMessage(输入字段名)对于非动态字段

When I post back to the server (I'm not using JSON just form post) I can do more complex validation and if something fails I can use ModelState.AddModelError("input field name", "I pity the fool that broke this"); This works perfectly with either strongly type or @Html.ValidationMessage("input field name") for non dynamic fields

不过,我无法找到一个方法来钩服务器端模型错误动态内容。

However I can't find a way to hook Server Side Model Error to dynamic content.

我有一个与客户方工作的跨度标签和他们很好地工作。但是他们没有得到挂钩到服务器端后,验证失败和返回页面返回的数据。任何想法如何达致这?

I have the span tags that work with the client side and they work perfectly. However they aren't getting hooked into data returned after serverside validation fails and return page. Any idea how to acheive this?

感谢

推荐答案

我刚刚完成了编码这种对我目前的工作的项目。我不能张贴code表示和解的规则。像上面您的评论说,没有一种优雅的方式。我将描述我们采取展示我们的错误信息的步骤。

I've just finished coding this for my current work project. I cannot post the code for propitiatory rules. Like your comment above says, there isn't an elegant way. I will describe the steps that we took to display our error messages.

首先,修改您的动态生成HTML,使每个人都有同等code到MVC3 @ Html.ValidationFor(...)的控制。接下来的每个动态控件需要有你可以用它来定位控件添加错误消息的ID字段。

First, modify your dynamically generated html so that each has the equivalent code to MVC3 @Html.ValidationFor(...) control. Next each dynamic control needs to have an id field that you can use to locate control to add error message to.

我的步骤进行了,从控制器接收到AJAX的数据进行验证后 -

Steps I took were, starting after the controller receives the ajax data for validation -


  1. 验证接收到的数据模型

  1. Validate the received data model

创建一个类来返回,看起来像这样的结果。

Create a class to return the results that looks like this

Class AjaxResults{
    bool success {get; set;);
    object returnedData {get; set;);
}


  • 如果模型验证,返回 AjaxResults 成功 = TRUE和 returnedData =验证数据模型

  • If model validates, return AjaxResults with success = true and returnedData = "validated data model"

    如果模型没有再验证

    收益 AjaxResults 成功 =虚假和 returnedData =错误清单

    return AjaxResults with success = false and returnedData = "list of errors"

    客户端接收 AjaxResults 对象后

    如果成功 = TRUE,处理结果正常。

    If success = true, process result normally.

    如果成功 =假,通过迭代列表突出有错误的领域和显示错误消息。

    If success = false, iterate through list highlighting the fields with the error and displaying the error message.

    在最后一步,你可以使用jQuery的验证消息显示错误code。如果你想这样做,那么在jquery.unobtrusive.valiation.js文件

    In the last step, you can use the jquery validation message display the error code. If you want to do this, then in jquery.unobtrusive.valiation.js file


    1. 添加code复制onError方法的功能性的文件中。

    2. 添加code到错误列表调用你onError方法来显示消息interate。请注意这里存储在错误消息跨度的.data属性信息。

    3. 您可能需要编写code在提交的形式来清除所有这些错误。

    这是一个相当长的过程。但是,code很容易模块化,可调用程序。我们目前在我们的生产code和在实践中使用此,它成为我们的框架code的一部分。

    This is a fairly long procedure. But the code is easily modularized into callable routines. We are currently using this in our production code and in practice, it becomes part of our framework code.

    希望这有助于。

    这篇关于我怎么ModelState.AddModel错误绑定到淘汰赛动态创建的验证跨度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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