模型列表的远程验证 [英] Remote Validation for LIST of MODELs

查看:27
本文介绍了模型列表的远程验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了以下教程:http://msdn.microsoft.com/en-us/library/gg508808%28VS.98%29.aspx

一切似乎都很好,但就我而言,字符串用户名总是返回空值.经过大量研究,我发现每个人都发现了 BIND 前缀.这在许多情况下都很好,但不是这种情况.我应该注意所有属性和名称都对齐,但是在我的 for 循环中,EditorFor 创建了一个 [i].Username 字段,这不会映射到任何模型属性.

And everything seemed fine, but in my case, string Username always comes back null. After tonnes of research, I found everyone discovered BIND Prefixes. That would be great in many circumstances, but not this one. I should note all properties and names line up, however in my for loop, the EditorFor creates a [i].Username field and this doesn't map to any model property.

问题: 我想我想将 [i].Username 映射到 Username,其中 i 是 0 到无穷大的任意数字,因此当它获取时,该值会正确传递给 Action.我该怎么做呢?如果这是错误的,我该如何针对表中的特定行进行验证?

QUESTION: I think I want to map [i].Username to Username where i is any number from 0-infinity, so when it GETS, the value is passed to the Action properly. How do I do this? If this is wrong, what do I do validate this for a specific row in a table?

@for (var i = 0; i < Model.Count; i++)
{
  BLAH BLAH BLAH CODE FOR BUILDING TABLE ROWS
  <td>
     @Html.EditorFor(modelItem => Model[i].Username)
  </td>                               
}

因为我在技术上可以拥有数百条记录,如果不是数千条记录,我宁愿没有所有 1000 条的绑定前缀.我在这里是否从根本上遗漏了什么?我是 ASP.NET MVC 的新手并且习惯了 WebForms,所以我觉得有时我会混合概念并混合一些完全错误的东西.

Since I could technically have HUNDREDS if not THOUSANDS of records, I would rather not had a binding PREFIX for all 1000. Am I fundamentally missing something here? I am new to ASP.NET MVC and am used to WebForms so I feel like sometimes I am mixing concepts and mashing up something that is entirely wrong.

我通过执行以下操作修复了它,但不确定这是否是最好的主意.我将参数设置为等于没有 [i] 前缀的 FieldName,但仍然检索带有 [i] 前缀的元素.Javascript 不是我的强项,所以如果它很糟糕,请告诉我.

I fixed it by doing the following, but not sure if this is the best idea. I set the parameter equal to the FieldName without [i] prefix, but still retrieve the element with the [i] prefix. Javascript isn't my Forte so please let me know if it is horrible.

adapters.add("remote", ["url", "type", "additionalfields"], function (options) {
    var value = {
        url: options.params.url,
        type: options.params.type || "GET",
        data: {}
    },
        prefix = getModelPrefix(options.element.name);

    $.each(splitAndTrim(options.params.additionalfields || options.element.name), function (i, fieldName) {

        var paramName = fieldName.substr(fieldName.lastIndexOf(".") + 1);

        var actualFieldName = appendModelPrefix(fieldName, prefix)
        value.data[paramName] = function () {
            return $(options.form).find(":input").filter("[name='" + escapeAttributeValue(actualFieldName) + "']").val();
        };
    });

    setValidationValues(options, "remote", value);
});

推荐答案

您尚未发布模型或控制器的代码,但假设您已将 RemoteAttribute 应用于属性 Username,例如

You have not posted your code for the model or controller, but assuming you have a RemoteAttribute applied to property Username, for example

public class MyModel
{
  [Remote("IsValidUserName", "Person")]
  public string Username { get; set; }
}

PersonController

public JsonResult IsValidUserName(string Username)
{
  ....
}

和视图

@model List<Person>
...
@for (var i = 0; i < Model.Count; i++)
{
  @Html.EditorFor(m => m[i].Username)                           
}

这将生成 html 如

This will generate html such as

<input name="[0].UserName" ... />
<input name="[1].UserName" ... />

不幸的是,jquery-validate 中的 remote 方法回发了元素的名称和值,因此 ajax 调用看起来像

Unfortunately the remote method in jquery-validate posts back the name and value of the element so that the ajax call looks like

$.ajax({
  url: '/Person/IsValidUserName',
  data: { [0].UserName: 'someone@somewhere.com' },
  ...

哪个不会绑定.

我已将此报告为 Codeplex 的问题,并提供了可能的解决方案.同时你可以修改jquery-validate.js文件中的remote方法如下

I have reported this as an issue at Codeplex with a possible solution. In the meantime you can modify the remote method in jquery-validate.js file as follows

remote: function(value, element, param) {
  ....
  var data = {};
  // data[element.name] = value;
  data[element.name.substr(element.name.lastIndexOf(".") + 1)] = value; // add this

这将去除前缀,以便发布的数据为

This will strip the prefix so that the posted data is

 data: { UserName: 'someone@somewhere.com' },

并将正确绑定到该方法.

and will correctly bind to the method.

这篇关于模型列表的远程验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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