对于asp mvc3客户端验证,是否可以将默认的,开箱即用的不引人注目的样式与jquery验证插件混合使用?
这是视图中的表单:
@using (Html.BeginForm("", "", FormMethod.Post, new { id = "newRatingForm" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Rating</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Rate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Rate)
@Html.ValidationMessageFor(model => model.Rate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
默认情况下,Unobtusive在模型属性上使用通常的验证属性。目标是用[fyneworks] [1] jquery星级评级插件替换表单中的'model.Rate'位,并使用jquery验证插件仅用于表单的那一部分:
<span>Your Rating:</span>
<div id="ratingStars">
<input class="star {split:2}" type="radio" name="Rating" value="0.5"/>
<input class="star {split:2}" type="radio" name="Rating" value="1.0"/>
<input class="star {split:2}" type="radio" name="Rating" value="1.5"/>
<input class="star {split:2}" type="radio" name="Rating" value="2.0"/>
<input class="star {split:2}" type="radio" name="Rating" value="2.5"/>
<input class="star {split:2}" type="radio" name="Rating" value="3.0"/>
<input class="star {split:2}" type="radio" name="Rating" value="3.5"/>
<input class="star {split:2}" type="radio" name="Rating" value="4.0"/>
<input class="star {split:2}" type="radio" name="Rating" value="4.5"/>
<input class="star {split:2}" type="radio" name="Rating" value="5.0"/>
</div>
我已经劫持了表单的提交操作并将表单值转换为Json以执行ajax请求:
$("#newRatingForm").submit(function (event) {
event.preventDefault();
if ($(this).valid()) {
newRatingSubmit();
}
});
function newRatingSubmit() {
//jquery ajax request using form values converted to Json
}
单选按钮似乎打破了表单,劫持代码不再有效,即当我点击提交时,没有向服务器发出请求(在firebug控制台上观看),表单就消失了。再一次,只用普通的mvc3格式就可以了。
我尝试为评级单选按钮添加jquery验证规则,但似乎没有任何区别:
$("#newRatingForm").validate({
meta: "validate",
errorLabelContainer: "#ErrorDiv",
wrapper: "div",
rules:
{
Rating: {
required: true
}
},
messages:
{
Rating: {
required: 'A Rating required.'
}
},
onfocusout: false,
onkeyup: false,
//handle validation ok, POST submit
submitHandler: function (label) {
newRatingSubmit();
}
});
作为替代方案,我可以使用jquery验证插件使用星级评级插件验证标准html表单,并使用上面的代码提交,但这似乎不是'asp.net mvc'。例如,我需要2组验证消息 - 1个用于服务器,使用验证属性,另一个用于客户端视图。
答案 0 :(得分:2)
jQuery.validator.addMethod("methodeName", function (value, element, params) {
if (somthing) {
return false;
}
return true;
});
jQuery.validator.unobtrusive.adapters.addBool("methodeName");
并在html中添加2个属性
dal-val
= true和dal-val-methodeName="errorMessage"
答案 1 :(得分:0)
正如@Vadim所建议的那样,我用data-val =“true”data-val-required =“请输入你的评分”来装饰我的表单的星级评级控件,如下所示;
<div id="ratingStars">
<input class="star {split:2}" type="radio" name="Rating" value="0.5" data-val="true" data-val-required="Please enter your rating"/>
<input class="star {split:2}" type="radio" name="Rating" value="1.0" data-val="true"data-val-required="Please enter your rating"/>
<input class="star {split:2}" type="radio" name="Rating" value="1.5" data-val="true" data-val-required="Please enter your rating"/>
<input class="star {split:2}" type="radio" name="Rating" value="2.0" data-val="true" data-val-required="Please enter your rating"/>
<input class="star {split:2}" type="radio" name="Rating" value="2.5" data-val="true" data-val-required="Please enter your rating"/>
<input class="star {split:2}" type="radio" name="Rating" value="3.0" data-val="true" data-val-required="Please enter your rating"/>
<input class="star {split:2}" type="radio" name="Rating" value="3.5" data-val="true" data-val-required="Please enter your rating"/>
<input class="star {split:2}" type="radio" name="Rating" value="4.0" data-val="true" data-val-required="Please enter your rating"/>
<input class="star {split:2}" type="radio" name="Rating" value="4.5" data-val="true" data-val-required="Please enter your rating"/>
<input class="star {split:2}" type="radio" name="Rating" value="5.0" data-val="true" data-val-required="Please enter your rating"/>
</div>
<span data-valmsg-replace="true" data-valmsg-for="Rating" class="field-validation-valid" style="display:block; width:80px"></span>
我认为我不需要自定义验证器,因为jquery默认的'required'验证器可以正常运行。这种方式有效。但是,我第一次点击提交,我得到了我希望的确切结果;
如果我然后选择评级并再次点击提交(不输入电子邮件),表单就会从视图中“消失”而我的控制器没有被点击。
如果我用纯文本框替换单选按钮而不是接受评级;
<input type="text" name="Rating" id="Rating" data-val-required="Please enter your rating" data-val="true" class="text-box single-line"/>
它工作得很好,当我点击提交时我的控制器被点击了。不幸的是,文本框对评级没有好处。