包含特殊字符的元素远程验证 [英] Remote Validation with elements containing special characters

查看:134
本文介绍了包含特殊字符的元素远程验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用内置到ASP.net MVC 3,我已经使用了一些其他时间成功的远程验证功能。然而,由于具有包含[和](一关的主力机型的集合)的名称在页面上的表单元素,我似乎无法使用远程验证。

这是被我的页面上呈现的HTML是:

 <输入ID =Leads_0__OpenDate级=日期选择器中hasDatepicker有效类型=文本名称=信息[0] .OpenDate数据-VAL-所需=开放日期是必需的。数据-VAL =真>

通常情况下,我想补充这我的视图模型为Opendate里场:

  [远程(ValidateOpenDate,机遇的ErrorMessage =开放日期不能在未来。)

然而,由于场信息的集合的一部分,它被回发到ValidateOpenDate动作为信息[0] .OpenDate,这是不把在参数的有效名称,由于C#没有按' ŧ允许[或]的变量名。

请问有什么方法可以让远程验证工作,而不是只写一些自定义JavaScript?


解决方案

 公众的ActionResult ValidateOpenDate()
{
    VAR Opendate里=请求[信息[0] .OpenDate];
    ...
}

或(未经测试,不知道是否会因为支架的工作):

 公众的ActionResult ValidateOpenDate([装订(preFIX =信息[0])字符串Opendate里)
{
    ...
}

I'm trying to use the Remote Validation feature built into ASP.net MVC 3, which I've used a few other times successfully. However, due to a form element on the page having a name that includes "[" and "]" (a collection off of the main model), I can't seem to use Remote Validation.

The Html that is being rendered on my page is:

<input id="Leads_0__OpenDate" class="datepicker medium hasDatepicker valid" type="text"  name="Leads[0].OpenDate" data-val-required="Open Date is required." data-val="true">

Usually, I would just add this to my ViewModel for the OpenDate field:

[Remote("ValidateOpenDate", "Opportunity", ErrorMessage = "The open date can not be in the future.")]

However, since the field is part of a collection of Leads, it gets posted back to the ValidateOpenDate action as "Leads[0].OpenDate", which is not a valid name to put in the parameters, since C# doesn't allow "[" or "]" in the variable name.

Is there anything I can do to get remote validation working, instead of just writing some custom javascript?

解决方案

public ActionResult ValidateOpenDate()
{
    var openDate = Request["Leads[0].OpenDate"];
    ...
}

or (untested, not sure if it will work because of the brackets):

public ActionResult ValidateOpenDate([Bind(Prefix = "Leads[0]")] string openDate)
{
    ...
}

这篇关于包含特殊字符的元素远程验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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