【问题标题】:Remote validation with radio button in asp.net mvc在 asp.net mvc 中使用单选按钮进行远程验证
【发布时间】:2013-11-06 15:59:00
【问题描述】:

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

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>

想法是,当布尔字段为真时,我需要一个日期。还有什么问题。当我提交表单时,它总是在我的 ValidMePlease 远程方法中为 IsSomething 传递 false。我认为这是因为只有第一个输入具有 data-val- 属性。任何建议将不胜感激。

【问题讨论】:

    标签: jquery asp.net-mvc jquery-validate


    【解决方案1】:

    看起来“远程”验证器中的 jquery.validate.unobtrusive.js 中存在错误。 尝试更换这一行:

    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();
    }
    

    您需要在两个单选按钮上使用相同的验证器,以确保在选择“是”选项时字段有效。

    另一件小事是确保 id 在您的 html 中是唯一的。从您的示例中,您有两个具有相同 id 属性的单选按钮。 我会使用这样的东西:

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

    添加:验证应该在不更改 id 属性的情况下工作。

    根据定义,ID 在任何系统中都必须是唯一的,因此通过确保它是唯一的, 不仅可以在针对 w3c.org 进行验证时为您提供帮助,还可以在为每个单选按钮添加标签时为您提供帮助。

    【讨论】:

    • 谢谢帮了我很多。我确实为多个单选按钮使用了相同的 ID,但效果仍然很好。
    • 我很高兴它帮助了@Julian。您是对的,唯一 ID 是此问题的可选步骤,无论如何,客户端验证都将按预期工作。我已经更新了我的答案来解释为什么 id 也应该被修改。
    • @mardok - 如果答案对您有用,请接受。
    猜你喜欢
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    相关资源
    最近更新 更多