文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

ASP.NET MVC 3 Remote

2023-01-31 00:55

关注

 This tutorial is related to the last one in that it has to do with validation on a registration request form. In the last post, CompareAttribute in ASP.NET MVC 3 - Compare Properties During Validation, I talked about validating that password and confirm password are equal using the new CompareAttribute in ASP.NET MVC 3. In this tutorial I will show you how to use remote validation in ASP.NET MVC 3 to let the user know if the username chosen during registration already exists using client-side validation.

Remote Validation in ASP.NET MVC 3

In this example we will be validating if a username provided by the user is unique during registration. Using the same example as in the past tutorial, here is a look at part of the RegisterRequest Class that is used during registration. Notice the Remote Attribute on the Username property. This signifies we want remote validation on this property and I have provided 3 parameters:

  • UsernameExists - the name of the action that will be called on my controller
  • Account - the name of the controller with the UsernameExists action
  • The error message I want displayed on the client if validation fails

Remote Validation in ASP.NET MVC 3

In addition to the Remote Attribute, we also need to create the controller action. Here is an example of UsernameExists on the Account Controller. You will obviously need to check your repository to see if the username exists. For simplicity I am just checking to see if the username provided equals “test”, and if so, returning false to tell the client that validation has failed.

ASP.NET MVC 3 Remote Validation

Since we are using JSON and I accepted the default of HTTP GET for remote validation, I also have to say that GET is an allowed behavior with JSON. If you want a POST to occur during remote validation, you can do so by setting HttpMethod = “POST” within the Remote Attribute.

The view for this is very basic as I am taking advantage of the built-in templating by ASP.NET MVC 3. Here is the view using the new Razor View Engine. 

ASP.NET MVC 3 Razor View Engine 

The end result is what you would expect. When a user chooses “test” for the username a validation request is fired to the server and an error is displayed with the error message we provided with the attribute. 

ASP.NET MVC 3 Remote Validation Tutorial Example Sample

If you look at the source you will see the HTML 5 data-* attributes used for private storage of information controlling the remote validation. In this case you will see:

  • data-val-remote="Username already exists"
  • data-val-remote-additionalfields="*.Username"
  • data-val-remote-url="/Account/UsernameExists"
  • data-val-remote-type="POST" ( if using POST )

If you are having problems with this, make sure you have client validation enabled as well an unobtrusive JavaScript enabled. I usually set them in the AppSettings, but there are HtmlHelpers you could use as well:

Enable client-side validation and unobtrusive JavaScript

You will also need to make sure you have the following JavaScript files linked in your layout or master page:

  • jquery-1.4.4.min.js
  • jquery.validate.min.js
  • jquery.validate.unobtrusive.min.js

Failure to enable the proper settings or link the proper JavaScript files will cause nothing to happen, filling your hours with all kinds of grief as to what could be the problem.

Conclusion

ASP.NET MVC 3 has excellent support for validation. Remote Validation is exremely simple and a worthwhile feature to provide in your web applications. Check out the previous tutorial on CompareAttribute in ASP.NET MVC 3 to get a feel for the whole registration requst process.

Hope this helps.

David Hayden

 

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯