asp.net mvc 3 中的远程验证问题
我有一个 Person 模型
public class Person
{
public int ID { get; set; }
[Required]
[Remote("UserNameExists", "People", "Username is already taken.")]
public string Name { get; set; }
[Required]
public string LastName { get; set; }
}
这是我的 UserNameExists 方法
public JsonResult UserNameExists(string name)
{
bool exists = personRepository.GetPersonByName(name.Trim());
if (!exists)
return Json(true, JsonRequestBehavior.AllowGet);
return Json(string.Format("{0} is not avavfddvilable.", name),
JsonRequestBehavior.AllowGet);
}
当我启用 Javascript 时,它工作得很好,但是当我禁用 javascript 时,此规则不会强制执行...
这是为什么?
请帮忙。
编辑预期行为:
根据msdn 即使没有 Javacript,它也应该尊重这个规则
- (可选)禁用浏览器中的客户端脚本,再次运行页面, 并输入违反规定的数据 验证约束。
当您离开包含以下内容的字段时 无效数据,您看不到 验证错误,因为脚本是 禁用。因为 ASP.NET MVC 使用的是 不显眼的 JavaScript,你看不到 客户端脚本错误。然而, 执行服务器端验证 当您提交表格时。 (这是一个 测试您的网络的好习惯 带有浏览器的应用程序 脚本已禁用。)
I have a Person Model
public class Person
{
public int ID { get; set; }
[Required]
[Remote("UserNameExists", "People", "Username is already taken.")]
public string Name { get; set; }
[Required]
public string LastName { get; set; }
}
This is my UserNameExists method
public JsonResult UserNameExists(string name)
{
bool exists = personRepository.GetPersonByName(name.Trim());
if (!exists)
return Json(true, JsonRequestBehavior.AllowGet);
return Json(string.Format("{0} is not avavfddvilable.", name),
JsonRequestBehavior.AllowGet);
}
When I have Javascript enabled it works just fine but when I disable javascript this rule is not enforced...
Why is this?
Please Help.
Edit for expected behavior:
According to msdn it should respect this rule even without Javacript
- Optionally, disable client script in your browser, run the page again,
and enter data that violates the
validation constraints.As you leave the field that contains
the invalid data, you do not see a
validation error because scripting is
disabled. Because ASP.NET MVC is using
unobtrusive JavaScript, you do not see
client-side script errors. However,
server-side validation is performed
when you submit the form. (It is a
good practice to test your Web
application with a browser that has
scripting disabled.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅我的 MSDN 文章如何:在 ASP 中实现远程验证。 NET MVC 我在 HttpPost Create 方法中使用远程客户端验证代码来测试禁用 JavaScript 时的服务器端。
See my MSDN article How to: Implement Remote Validation in ASP.NET MVC I use the remote client validation code in the HttpPost Create method to test server side when JavaScript is disabled.
您必须在服务器上复制验证调用 - 这不会按照我的测试所述工作。
请参阅我的帖子:
ASP.NET MVC 3 中的干远程验证
You must duplicate a validation call on the server - this DOES NOT work as outlined as per my testing.
See my post at:
DRY Remote Validation in ASP.NET MVC 3
听起来您已禁用 JavaScript,并且远程验证失败。
远程验证需要在浏览器中启用 JavaScript。它使用 jQuery 和 AJAX 调用来完成此任务。
MSDN 中的引用正是您所观察到的:
It sounds like you have JavaScript disabled, and your Remote Validation fails.
Remote Validation requires JavaScript enabled in the browser. It's using jQuery and an AJAX call to get this done.
The quote from MSDN is exactly what you're observing: