[英]Use Remote validation in ASP.NET MVC without form on View
I use Remote
attribute on my ViewModel
in ASP.NET MVC 4 application. 我在ASP.NET MVC 4应用程序的
ViewModel
上使用Remote
属性。 Here is my simple model: 这是我的简单模型:
public class User
{
[Required(AllowEmptyStrings = false, ErrorMessage = "test test")]
public int Id { get; set; }
[DisplayName("Email")]
[Remote("RemoteValidateEmailLengthValidation", "Home")]
public string Email { get; set; }
}
And remote validation method: 和远程验证方法:
public JsonResult RemoteValidateEmailLengthValidation(string Email)
{
if (Email.Length > 20)
{
return Json("Too long email", JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
I added all scripts that i need for validation on Layout
: 我在
Layout
上添加了验证所需的所有脚本:
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/bundles/jqueryval")
Write all that i need for validation in WebConfig
: 在
WebConfig
写下验证所需的全部内容:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
And add simple Controller
that creates empty model and return View
. 并添加创建空模型并返回
View
简单Controller
。
@model ViewModels.User
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("PostUser", "Home", FormMethod.Post))
{
@Html.EditorForModel()
<input type="submit" />
}
It works only if i wrap my model in form
and doesn't work if i write my View
like this: 仅当我将模型包装在
form
它才起作用,而当我这样编写View
不起作用 :
@model ViewModels.User
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@Html.EditorForModel()
So i have questions: 所以我有问题:
OnChange
? OnChange
? Can i see it anywhere and can i change it? Client side unobtrusive validation involves 客户端非侵入式验证涉及
HtmlHelper
. HtmlHelper
的GetUnobtrusiveValidationAttributes方法。 Various checks are performed and if not all conditions are met (such as UnobtrusiveValidation has been disabled) then the data-val
attributes necesary for client side validation are not rendered data-val
属性 jquery.validation.unobtrusive.js
is loaded, it first checks for the existence of a <form>
tag, then based on the data-val
attributes, add the rules,messages etc. for use by jquery.validate
. jquery.validation.unobtrusive.js
,它首先检查<form>
标记的存在,然后基于data-val
属性,添加供jquery.validate
使用的规则,消息等jquery.validate
。 The first function called is 第一个调用的函数是
parse: function (selector) {
var $forms = $(selector)
.parents("form")
.andSelf()
.add($(selector).find("form"))
.filter("form");
....
where selector
is the html document element. 其中
selector
是html文档元素。 If there is no <form>
element, var $forms
is undefined and nothing more is executed. 如果没有
<form>
元素,则var $forms
是未定义的,仅执行其他操作。 Client side validation simply does not work if there is no <form>
element. 如果没有
<form>
元素,则客户端验证根本无法工作。
Its unclear why you would generate html form controls that are not in a form, but you could simply make your own call to the controller function, return a message and display it 目前尚不清楚为什么会生成不在表单中的html表单控件,但是您可以简单地对控制器函数进行调用,返回一条消息并显示它
public JsonResult RemoteValidateEmailLengthValidation(string Email)
{
if (Email.Length > 20)
{
return Json("Too long email", JsonRequestBehavior.AllowGet);
}
else
{
return Json(null, JsonRequestBehavior.AllowGet);
}
}
and the script 和脚本
var url = '@Url.Action("RemoteValidateEmailLengthValidation", "Home")';
var placeHolder = $('[data-valmsg-for="Email"]');
$('#Email').change(function() {
$.getJSON(url, { Email: $(this.val() }, function(response) {
if(response) {
placeHolder.text(response).removeClass('field-validation-valid').addClass('field-validation-error');
}
});
});
and also handle the .keyup
event to remove error message and reset the class name 并处理
.keyup
事件以删除错误消息并重置类名
Cant find JsonRequestBehavior in aspnet 5 无法在ASPNET 5中找到JsonRequestBehavior
[HttpGet]
public JsonResult IsAllowedName(string FirstMidName)
{
if (FirstMidName.ToLower() == "oleg")
{
//It seems that Microsoft.Asp.Net.Mvc does
//not contain JsonRequestBehavior enum
return Json(false, JsonRequestBehavior.AllowGet);
}
return Json(true);
}
Terminal output 终端输出
dnu build
/.../Controllers/ValidationController.cs(20,24):
DNXCore,Version=v5.0 error CS0103:
The name 'JsonRequestBehavior' does not exist in the current context
Build failed.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.