在asp.net mvc的无线电按钮遥控验证 [英] Remote validation with radio button in asp.net mvc

查看:308
本文介绍了在asp.net mvc的无线电按钮遥控验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的布尔型字段使用远程验证。我有一个看起来像这样的视图模型:

I need to use remote validation on my boolean field. I have view model which look like this:

public class ViewModel
{   
    [Remote("ValidMePlease", "Home", AdditionalFields = "SomeKindOfDate", ErrorMessage = "use date with true")]
    public bool IsSomething {get;set;}
    public DateTime? SomeKindOfDate {get;set;}
}

我的控制器:

[OutputCache(Location = System.Web.UI.OutputCacheLocation.None, NoStore = true)]
public class HomeController : Controller 
{
    public JsonResult ValidMePlease(bool IsSomething , DateTime? SomeKindOfDate )
    {
        if (IsSomething && !SomeKindOfDate .HasValue)
            return Json(false, JsonRequestBehavior.AllowGet);
        return Json(true, JsonRequestBehavior.AllowGet);
    }
}

我的观点:

<div class="control">
    @Html.LabelFor(m => m.IsSomething , new { @class = "control-label" })
    <div class="controls">
        <label class="radio inline">
            @Html.RadioButtonFor(m => m.IsSomething , false) No
        </label>
        <label class="radio inline">
            @Html.RadioButtonFor(m => m.IsSomething, true) Yes
        </label>
    </div>
</div>

我的渲染视图:

   <div class="control">
        <label class="control-label" for="IsSomething ">Is something</label>
        <div class="controls">
            <label class="radio inline">
                <input checked="checked" data-val="true" data-val-remote="use date with true" data-val-remote-additionalfields="*.IsSomething ,*.SomeKindOfDate" data-val-remote-url="/admin/User/ValidMePlease" data-val-required="Is something is required." id="IsSomething " name="IsSomething " type="radio" value="False" /> No
            </label>
            <label class="radio inline">
                <input id="IsSomething " name="IsSomething " type="radio" value="True" /> Yes
            </label>
        </div>
</div>

想法是,当布尔字段有真我需要需要一个日期。而且什么问题。当我提交形式,它总是传递 对于 IsSomething 在我的 ValidMePlease 远程方法。我想这是因为只有先输入有 数据-VAL - 属性。任何建议将AP preciated。

Idea is that when boolean field has true I need to require a date. And what's the problem. When I submit form it always pass false for IsSomething in my ValidMePlease remote method. I think it happens because only first input has data-val- attributes. Any suggestion will be appreciated.

推荐答案

看起来像有一个在jquery.validate.unobtrusive.js在远程验证器的错误。
也许尝试替换这一行:

Looks like there is a bug in the jquery.validate.unobtrusive.js in the "remote" validator. Try maybe replacing this line:

return $(options.form).find(":input[name='" + escapeAttributeValue(paramName) + "']").val();

var $input = $(options.form).find(":input[name='" + escapeAttributeValue(paramName) + "']");
if ($input.is(":radio")) {
    return $input.filter(":checked").val();
} else {
    return $input.val();
}

您需要在两个单选按钮同样的验证,要选择的时候一定领域有效Yes选项。

You will need same validator on both radio buttons, to make sure field is valid when selected Yes option.

另外一个小的事情是确保ID是在你的HTML是独一无二的。从你的例子中,你拥有相同id属性这两个单选按钮。
我会用这样的:

Another small thing is making sure id is unique in your html. From your example you have both radio buttons with the same id attribute. I would use something like this:

@Html.RadioButtonFor(m => m.IsSomething, false, new { id = "IsSomething_False" })
@Html.RadioButtonFor(m => m.IsSomething, true, new { id = "IsSomething_True" })

补充:确认应该没有改变id属性

Added: Validation should work without changing id attribute.

由它定义ID必须是在任何系统中唯一的,所以通过确保它是独一无二的,
将帮助你不仅在添加标签到每个单选按钮时验证对w3c.org,也。

ID by its definition has to be unique in any system, and so by making sure it is unique, will help you not only when validating against w3c.org, but also when adding labels to each radio button.

这篇关于在asp.net mvc的无线电按钮遥控验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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