iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >ASP.NET MVC 3 Remote
  • 146
分享到

ASP.NET MVC 3 Remote

NETASPRemote 2023-01-31 00:01:33 146人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

 This tutorial is related to the last one in that it has to do with validation on a reGIStration request fORM. In the la

 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

 

--结束END--

本文标题: ASP.NET MVC 3 Remote

本文链接: https://www.lsjlt.com/news/182855.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • ASP.NET MVC 3 Remote
     This tutorial is related to the last one in that it has to do with validation on a registration request form. In the la...
    99+
    2023-01-31
    NET ASP Remote
  • ASP.NET MVC 3中ViewBa
    ViewBag, ViewData十分类似,都可用于把数据从controller传递到view。 ViewBag是WebViewPage中的一个属性,它的类型是dynamic。dynamic类型可以理解为,编译器在编译到这种类型时,会跳过类...
    99+
    2023-01-31
    NET ASP ViewBa
  • 如何理解ASP.NET MVC 3 Beta中的Chart
    这篇文章给大家介绍如何理解ASP.NET MVC 3 Beta中的Chart,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。小编将介绍Chart的使用。包括Chart数据源的配置、Chart的显示、Chart保存三个方面...
    99+
    2023-06-17
  • ASP.NET MVC2项目如何升级到MVC 3 RC
    这篇文章主要讲解了“ASP.NET MVC2项目如何升级到MVC 3 RC”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“ASP.NET MVC2项目如何升级到MVC 3 RC”吧!要将现有的...
    99+
    2023-06-17
  • ASP.NET MVC 3中的Razor特性怎么使用
    本篇内容主要讲解“ASP.NET MVC 3中的Razor特性怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“ASP.NET MVC 3中的Razor特性怎么使用”吧!开工 目录...
    99+
    2023-06-17
  • ASP.NET MVC 3中如何理解控制器与视图
    本篇文章给大家分享的是有关ASP.NET MVC 3中如何理解控制器与视图,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。I:控制器和视图的基础概念控制器的概念控制器在ASP.N...
    99+
    2023-06-17
  • ASP.NET MVC 3新特性与NuPack功能是怎样的
    这篇文章将为大家详细讲解有关ASP.NET MVC 3新特性与NuPack功能是怎样的,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。NuPack ; .NET的开源软件包管理器NuPack是...
    99+
    2023-06-17
  • 如何用ASP.NET MVC 3实现一个访问统计系统
    本篇文章为大家展示了如何用ASP.NET MVC 3实现一个访问统计系统,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。运营网站,我们经常需要分析用户的行为、用户的习惯,用户看重网站的哪一部分,哪一部...
    99+
    2023-06-17
  • ASP.NET MVC 3如何让依赖注入实现得更简单
    本篇文章为大家展示了ASP.NET MVC 3如何让依赖注入实现得更简单,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。我没有找到一个完整的示例,只有一些代码片断,于是,我将其整理了一翻,也有一点点个...
    99+
    2023-06-17
  • ASP.NET MVC是什么
    本篇内容主要讲解“ASP.NET MVC是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“ASP.NET MVC是什么”吧!首先,我们来看Imar提供的程序,这是一个具有典型增删查改的程序,这...
    99+
    2023-06-17
  • ASP.NET 缓存(3)
    有2种方式来实现缓存部分页。 片段缓存:这种情况下,你把确定要缓存的内容,包裹在一个专用的用户控件里,然后只需要对这个控件做输出缓存就行。 post-cache substitution 缓存后替换。在这种情况下,你...
    99+
    2023-01-31
    缓存 ASP NET
  • 为什么ASP.NET MVC 3不安装也能在服务器上运行
    这篇文章将为大家详细讲解有关为什么ASP.NET MVC 3不安装也能在服务器上运行,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。当在服务器上部署 ASP.NET MVC3 的应用的时候,可...
    99+
    2023-06-17
  • ASP.NET MVC框架简介
    一、MVC模式 概述 1、MVC是一种流行的Web应用架构技术,他把Web应用划分成Model(模型)、Controller(控制器)和View(视图)三部分。 2、优点:可测试、可...
    99+
    2022-11-13
  • ASP.NET MVC模式简介
    目录一、MVC模式简介1、MVC在Web框架中的应用2、执行顺序二、ASP.NET中的MVC1、ASP.NET MVC和ASP.NET2、ASP.NET MVC和WebForms三、...
    99+
    2022-11-13
  • ASP.NET Core MVC 过滤器(Filter)
    目录一.过滤器如何工作1.选择过滤器2.实现过滤器3.过滤器作用域 4.取消和短路二.配置过滤器1.依赖注入2.排序 3.对比中间件一.过滤器如何工作   不同的...
    99+
    2022-11-12
  • spring mvc 3 最简单demo
    这两天再看spring mvc,想再网上找一个demo,感觉国内的demo都太复杂了。后来在国外网站上发现了一个比较简单,应该说是最简单的spring mvc的demo了,在此做个记录,给需要的人了解一下。 第一步:准备包: 日志相关包 j...
    99+
    2023-01-31
    最简单 spring mvc
  • asp.net EF+MVC2实战3
    如果一个存储过程获得的是两张表的联合查询结果,那么我们用EF该如何映射存储过程呢?很简单,再建一个视图。存储过程如下 ALTER    PROCEDURE    [dbo].[getFuncsByUserAuth]     @strUse...
    99+
    2023-01-31
    实战 net asp
  • C#, ASP.NET笔试题(3)
    本文转自:http://www.cnblogs.com/JCSU/articles/1289762.html120. 请叙述属性与索引器的区别 属性: (1) 通过名称标识 (3) 通过简单名称或成员访问来访问...
    99+
    2023-01-31
    笔试 ASP NET
  • ASP.NET MVC中如何使用MvcPager
    这篇“ASP.NET MVC中如何使用MvcPager”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来...
    99+
    2022-10-19
  • ASP.NET Core MVC中的布局(Layout)
    目录1.什么是布局2.指定布局* 部分*忽略部分3.导入共享指令4.在每个视图之前运行代码1.什么是布局 布局通常包含常见的用户界面元素,例如应用头,导航或菜单元素和页脚,如图: ...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作