mvc5

ASP.NET MVC5(四):数据注解和验证

被刻印的时光 ゝ 提交于 2019-12-26 17:25:39
前言   用户输入验证的工作,不仅要在客户端浏览器中执行,还要在服务端执行。主要原因是客户端验证会对输入数据给出即时反馈,提高用户体验;服务器端验证,主要是因为不能完全信任用户提供的数据。ASP.NET MVC框架提供了强大的验证组件帮助我们处理这些繁杂的问题。 数据验证 验证注解的使用   验证注解特性定义在命名空间System.ComponentModel.DataAnnotations中,它们提供了服务器端验证的功能,当在模型的属性上使用时,框架也支持客户端验证。常用特性简介: Required 当属性值为null或者空时,将引发一个验证错误,可以理解为若添加了Required特性,则此项为必填项。 StringLength 限定字符串长度。 RegularExpression 使用正则表达式验证输入的字符串是否符合格式要求。 Range 用来指定输入数值来的最小值和最大值。 Compare 用来判断两个属性是否拥有相同的值。例如,确保两次输入的密码相同。 Remote 利用服务器端的回调函数执行客户端的逻辑验证。 下面,通过一个简单的示例来讲解这些特性的使用方法。 假设现在我们开发一套图书管理系统,在Models文件夹中创建Book类,用来保存书籍的基本信息。 public class Book { public int Id { get; set; } public

MVC5的AuthorizeAttribute详解

半腔热情 提交于 2019-12-23 00:14:05
现今大多数的网站尤其是购物网站都要求你登录后才能继续操作,当你匿名的将商品放入购物车后,不可能匿名购买这时可以转到登录界面让用户进行登录验证。 适用系统自带的过滤器 MVC5只要将属性[Authorize]置于相关的action之前就行,那么在调用Buy action之前,就会运用Authorize过滤器。 1 [Authorize] 2 public ActionResult Buy(int id) 3 { 4 //其它购买逻辑代码放在这里 5 6 }   2. 也可以将属性[Authorize]置于整个Controller之上。这样位于此控制器下的所有action就都运用了此过滤器。 [Authorize] public class UserController : Controller { //一些action }   3. 也可以将Authorize应用到全部的应用程序的范围类,要使AuthorizeAttribute成为全程序的过滤器,只要将其加入全局过滤器集合RegisterGlobalFilters方法,这个方法位于\App_Start\FilterConfig.cs: public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new System

MVC5 + EF6 入门完整教程

爱⌒轻易说出口 提交于 2019-12-19 02:23:09
原文: MVC5 + EF6 入门完整教程 第0课 从0开始 ASP.NET MVC开发模式和传统的WebForm开发模式相比,增加了很多"约定"。 直接讲这些 "约定" 会让人困惑,而且东西太多容易忘记。 和微软官方教程不同,笔者尽量不用脚手架,从空白框架开始,一步一步添加功能,每次添加的东西刚好够用,让大家能真正能用起来,理解每一个过程。 文章提纲 概述 核心概念介绍 从空白开始,建立一个基本框架详细步骤 概述 本系列文章及文章中的例子主要基于微软官方文档 使用工具 : VS2013 + MS SQL 2012 开始主要讲解MVC + EF搭配使用,后续同样也会提供MVC + ADO.NET的实现方案 核心概念介绍 MVC,Model – View – Controller 的简写 Model 封装业务逻辑相关的数据及对数据的处理方法 View 向用户提供交互界面 Controller 负责控制Model和View 看下面这张图。目前只要理解这一个概念就可以了,下面就开始建一个空框架,从做中学。 从空白开始,建立一个基本框架详细步骤 新建项目 NOTE:模板要选Empty,如果直接选MVC会产生多余代码。 NOTE:上图方框处正好对应于M, V, C 到此为止,就建立了一个最基本的MVC解决方案,基本是空的。 我们简单介绍下其中的RouteConfig.cs文件

ASP.NET MVC5基础-控制器(Controller)详解

試著忘記壹切 提交于 2019-12-06 02:20:10
在上文 ASP.NET MVC5基础 – MVC文件架构 中我们简单了解了下控制器Controller的作用,本文我将详细介绍控制器Controller的使用方法。 Controller的运行过程 上文我们已经讲到,控制器负责响应浏览器传送过来的所有请求。在MVC中,每一个浏览器请求都映射到一个控制器(Controller)中,每个请求都会有个动作(Action),只要动作存在,就可以通过该动作方法接收客户端传来的请求与决定响应的视图(View)。 我们以之前创建的MVC项目为例,打开项目的Index.cshtml页面。 显示的界面如下: 可以看到,页面的地址栏URL为: http://localhost:61515/Home/Index 根据路由规则,Home是控制器(Controller)名,Index是动作(Action)名。所以这个URL调用的是HomeController控制器下的Index方法。 在Index方法中,只有这么一行代码: return View(); ,表示返回视图,然后返回给浏览器的是Views/Home文件夹下的Index.cshtml页面。 虽然我们在Index方法中没有指定返回哪个页面,但根据ASP.NET MVC的约定规则,控制器会找到Views文件夹中,与Controller名称相同文件夹下的同一方法名的页面。所以返回的是Home下的Index

MVC5 view drop down list

匿名 (未验证) 提交于 2019-12-03 02:54:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In a C# MVC5 Internet application view, how can I display a dropdown list for a user to select a list item that is populated from a View Model list? Here is the ViewModel code: public class MapLocationItemViewModel { [Editable(false)] public int mapLocationForeignKeyId { get; set; } public List<string> mapLocationItemTypes { get; set; } public MapLocationItem mapLocationItem { get; set; } } Here is the code that I currently have in the View : <div class="form-group"> @Html.LabelFor(model => model.mapLocationItem.mapLocationItemType, new {

How to add ASP.NET MVC5 Identity Authentication to existing database

匿名 (未验证) 提交于 2019-12-03 02:50:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am learning MVC5 identity authentication and was reading materials on www.asp.net. I have a few questions here. If I want to use identity authentication, is there a reason not to use MVC template? Or is there a reason to use empty template? MVC template also provides bootstrap. I have a database created, I want to have a DB first development. If I use the MVC template, the database for credentials will be created under the project folder. How can I merge the two database or I should just use two databases? If my question is silly, just

ASP.NET MVC5/IIS Express unable to debug - Code Not Running

匿名 (未验证) 提交于 2019-12-03 02:42:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I start a VS2013 VB.Net MVC5 Web Application, set a breakpoint in HomeController About method and run "Start Debugging", navigate to About page and receive the message "Code not running - The current Thread is not currently running or the call stack could not be obtained" preventing any debugging. If I set a breakpoint in Global.asax Application_Start, the breakpoint works fine. Changing the "Enable Edit & Continue" makes no difference. Anyone have any ideas what is happening? Update : My environment was Windows 8.0 Pro with VS2013 Ultimate.

User.Identity.Name full name mvc5

匿名 (未验证) 提交于 2019-12-03 02:14:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've extended the ASP NET identity schema by adding few fields in the ApplicationUser Class which is derived from IdentityUser . One of the field that I've added is FullName . Now, when I write User.Identity.Name , it gives me the user name, I m looking for something like User.Identity.FullName which should return the FullName that I have added. Not sure, how this can be achieved any guidance shall be greatly appreciated. Thanks. 回答1: You could add it to the User's claims when you create the user and then retrieve it as a claim from the User

MVC5, AjaxHelper, and the Correct Scripts &amp; Load Order

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: OK, I find it EXTREMELY ridiculous that I have to post a new question to find the answer but, alas here I am. Let's make this as simple as possible for the next wayward soul looking to resolve this. What are all the most current scripts I require to get the ajax form working? Thus far I have; <script src = "//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.js" ></script> <script src = "//ajax.aspnetcdn.com/ajax/jquery.ui/1.10.4/jquery-ui.min.js" ></script> <script src = "//ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate

MVC5 Url.Action returns null inside area

匿名 (未验证) 提交于 2019-12-03 01:26:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: We have a MVC application with different areas. At moment just one area is registered (defined in config) and no route registrations is done in area registration. I have run into issues with routing as Url.Action method stopped to work. My simplified RouteConfig looks like this: routes.MapRoute( name: "Second", url: "second/{action}/{id}", defaults: new { controller = "Second", action = "Action", id = UrlParameter.Optional }, namespaces: new[] { "MvcApplication1.Areas.MyArea.Controllers" } ).DataTokens["area"] = "MyArea"; routes.MapRoute(