MVC4远程验证未接收参数值 [英] MVC4 Remote Validation Not Receiving Parameter Value

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

问题描述

我想实现远程验证了在视场。到目前为止的所有工作,除了在验证控制器方法的参数为null,即使该字段包含一个值。我错过了什么?

验证控制器方法

 公共JsonResult IsVanityURL_Available(字符串VanityURL)
{
    如果(!_webSiteInfoRepository.GetVanityURL(VanityURL))
        返回JSON(真,JsonRequestBehavior.AllowGet);    字符串suggestedUID =的String.Format(CultureInfo.InvariantCulture,
        {0}不可用。VanityURL);    的for(int i = 1; I< 100;我++)
    {
        字符串altCandidate = VanityURL + i.ToString();
        如果(_webSiteInfoRepository.GetVanityURL(altCandidate))继续;
        suggestedUID =的String.Format(CultureInfo.InvariantCulture,
            {0}不可尝试{1}。VanityURL,altCandidate);
        打破;
    }
    返回JSON(suggestedUID,JsonRequestBehavior.AllowGet);
}

实体属性

  [DisplayName的(个性化网址)]
[远程(IsVanityURL_Available,验证)]
[RegularEx pression(@(\\ S)+的ErrorMessage =白色空间是不允许的。)
.VanityURL)
        < / DIV>
        < D​​IV CLASS =输入组利润率底小>
            <跨度类=输入组插件>< I类=发FA-外部链接方FA-FW>< I&GT /;< / SPAN>
            @ Html.TextBoxFor(型号=> model.SelectedContact.WebSiteInfoes [0] .VanityURL,新{@class =表格控,@placeholder =输入个性化网址})
        < / DIV>
    < / DIV>
< / DIV>

更新
在后一式两份的答案确实解决问题。


解决方案

我发现的另一种方法,以避免更改jquery.validate.js文件。这涉及到像这样在视图中设置TextBoxFor的名字......

  @ Html.TextBoxFor(型号=> model.SelectedContact.WebSiteInfoes [0] .VanityURL,新{@class =表格控,@placeholder =输入个性化网址@名称=VanityUrl})

我恢复了我的js文件的改变,然后添加视图名称属性和模型遥控AdditionalFields定义的组合,它的工作就好了。

这变化造成了一些不可预见的问题,以及。我终于没有得到解决。我改变了得到一个POST,抓起我从FormsCollection所需的值。这<一个href=\"http://crea8ive$c$c.net/weblog/index.php/remote-validation-in-mvc3-simple-way-to-pass-the-form-value-from-custom-view-model-to-controller-via-remote-attribute/\"相对=nofollow>链接让我去在正确的方向。这让我完全绕过复杂的数据对象命名问题

I'm trying to implement Remote Validation for a field in a view. Everything so far is working except the parameter in the validation controller method is null even though the field contains a value. What did I miss?

Validation Controller Method

public JsonResult IsVanityURL_Available(string VanityURL)
{
    if (!_webSiteInfoRepository.GetVanityURL(VanityURL))
        return Json(true, JsonRequestBehavior.AllowGet);

    string suggestedUID = String.Format(CultureInfo.InvariantCulture,
        "{0} is not available.", VanityURL);

    for (int i = 1; i < 100; i++)
    {
        string altCandidate = VanityURL + i.ToString();
        if (_webSiteInfoRepository.GetVanityURL(altCandidate)) continue;
        suggestedUID = String.Format(CultureInfo.InvariantCulture,
            "{0} is not available. Try {1}.", VanityURL, altCandidate);
        break;
    }
    return Json(suggestedUID, JsonRequestBehavior.AllowGet);
}

Entity Property

[DisplayName("Vanity URL")]
[Remote("IsVanityURL_Available", "Validation")]
[RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed.")]
[Editable(true)]               
public string VanityURL { get; set; }

View

<div class="row">
    <div class="form-group col-md-12">
        <div class="editor-label">
            @Html.LabelFor(model => model.SelectedContact.WebSiteInfoes[0].VanityURL)
        </div>
        <div class="input-group margin-bottom-small">
            <span class="input-group-addon"><i class="fa fa-external-link-square fa-fw"></i></span>
            @Html.TextBoxFor(model => model.SelectedContact.WebSiteInfoes[0].VanityURL, new { @class = "form-control", @placeholder = "Enter Vanity URL" })
        </div>
    </div>
</div>

UPDATE The answer in the duplicate post does fix the problem.

解决方案

I found an alternate way to avoid changing the jquery.validate.js file. This involved setting the name of the TextBoxFor in the view like so...

@Html.TextBoxFor(model => model.SelectedContact.WebSiteInfoes[0].VanityURL, new { @class = "form-control", @placeholder = "Enter Vanity URL", @Name="VanityUrl })

I reverted my js file change, then added a combination of the view name attribute and the model remote AdditionalFields definition and it worked just fine.

This change caused some unforeseen problems as well. I finally did get a solution. I changed the GET to a POST and grabbed the values I needed from the FormsCollection. This link got me going in the right direction. This allowed me to completely bypass the Complex Data Object naming problem

这篇关于MVC4远程验证未接收参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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