问题描述
我用下面的教程:
和一切似乎都很好,但在我的例子中,字符串用户名总是回来空。经过研究万吨,我发现大家发现BIND prefixes。这将在许多情况下是巨大的,但不是这一个。我要指出的所有属性和名称在我的for循环中,EditorFor创建[I] .Username然而现场排队,这不映射到任何模型属性。
问:我想我要地图[I] .Username到名,其中i为任意数量从0无穷远,所以当它得到的值传递给正确的行动。我该怎么做呢?如果这是错的,我该怎么办验证此为表中的特定行?
@for(VAR I = 0; I< Model.Count;我++)\r
{\r
等等等等等等code建筑表行\r
&所述; TD>\r
@ Html.EditorFor(modelItem =>产品型号[I] .Username)\r
< / TD>\r
}
\r
因为我可以在技术上有数百如果不是成千上万的记录,我宁愿没有具有约束力的preFIX所有1000我在这里从根本上失去了一些东西?我是新来的ASP.NET MVC并习惯了对WebForms的,所以我觉得自己有时候我混合概念和混搭的东西是完全错误的。
编辑:
我固定它通过执行以下,但不知道这是否是最好的主意。我设定的参数等于没有[I] preFIX的字段名,但仍检索与[I] preFIX的元素。 JavaScript是不是我的专长,所以请让我知道,如果它是可怕的。
adapters.add(远程,[网址,型,additionalfields],功能(选件){
VAR值= {
网址:options.params.url,
类型:options.params.type || 得到,
数据:{}
},
preFIX = getModel preFIX(options.element.name); $。每个(splitAndTrim(options.params.additionalfields || options.element.name),功能(I,字段名){ 变种paramName配置= fieldName.substr(fieldName.lastIndexOf()+1。); VAR actualFieldName = appendModel preFIX(字段名,preFIX)
value.data [paramName配置] =功能(){
。返回$(options.form).find(:输入)过滤器([名='+ escapeAttributeValue(actualFieldName)+'])VAL();
};
}); setValidationValues(选项,远程,值);
});
您还没有发布您的code为模型或控制器,但假设你有一个 RemoteAttribute
适用于财产用户名
,例如:
公共类为MyModel
{
[远程(IsValidUserName,人)]
公共字符串用户名{获得;组; }
}
在 PersonController
公共JsonResult IsValidUserName(字符串用户名)
{
....
}
和视图
@model名单<&人GT;
...
@for(VAR I = 0; I< Model.Count;我++)
{
@ Html.EditorFor(M = GT; M [] .Username)
}
这将生成html如
<输入名称=[0] .UserName... />
<输入名称=[1] .UserName... />
不幸的是,远程
方法 jQuery的-验证
回发的名称和元素的值,使得AJAX调用看起来像
$。阿贾克斯({
网址:'/人/ IsValidUserName',
数据:{[0] .UserName:[email protected]},
...
这将不绑定。
我报告这是在codePLEX 的问题有可能解。在此期间,你可以修改远程
方法 jQuery的-validate.js
文件如下:
远程:函数(值,元素,参数){
....
VAR数据= {};
//数据[element.name] =价值;
数据[element.name.substr(element.name.lastIndexOf()+1。)] =值; //添加此
,以使发布的数据是这将去掉preFIX
数据:{用户名:[email protected]},
和将正确地绑定到方法
I used the following tutorial:http://msdn.microsoft.com/en-us/library/gg508808%28VS.98%29.aspx
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.
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>
}
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.
EDIT: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);
});
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; }
}
with a method in PersonController
public JsonResult IsValidUserName(string Username)
{
....
}
and the view
@model List<Person>
...
@for (var i = 0; i < Model.Count; i++)
{
@Html.EditorFor(m => m[i].Username)
}
This will generate html such as
<input name="[0].UserName" ... />
<input name="[1].UserName" ... />
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: '[email protected]' },
...
which will not bind.
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: '[email protected]' },
and will correctly bind to the method.
这篇关于对于型号列表远程验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!