asp.net Core MVC 有特别处理form,controller可以自己处理model的验证,最大的优势是写form时可以少写代码

先了解tag helper ,这东西就是element上的attribute,在angular来说就是指令,通过指令可以很好的写代码

cshtml

@model Project.Web.Home.Product

<form class="myForm" asp-controller="Home" asp-action="Form" method="post">
<!-- Input and Submit elements -->
<label asp-for="name"></label>
<input asp-for="name" class="form-control" />
<button type="submit">Submit</button>
<span asp-validation-for="name"></span>⁠⁠⁠⁠
</form>

Controller

public class Product
{
[Required(ErrorMessage = "Required")]//可以验证
[Display(Name = "Product Name")]
public string name { get; set; }
}

编辑后的cshtml

<form class="myForm" method="post" action="/form" novalidate="novalidate">
<!-- Input and Submit elements -->
<label for="name">Product Name</label>
<input class="form-control" type="text" data-val="true" data-val-required="Required" id="name" name="name" value="keat">//先忽略keat
<button type="submit">Submit</button>
<span class="field-validation-valid" data-valmsg-for="name" data-valmsg-replace="true"></span>⁠⁠⁠⁠
</form>

  

通过以上的代码,结果可以看出tag helper大致上处理了一些代码,还需要最后的validation和 model binding

Controller

public class HomeController : Controller
{
[Route("")] //忽略
[Route("{cnOrAmp:regex(^cn$|^amp$)}")]//忽略
public IActionResult Index(string cnOrAmp = "en")
{
var vm = new HomeVm();
vm.product = new Product();
vm.product2 = new Product2();//忽略
vm.product.name = "keat";
vm.product2.name2 = "mark";//忽略 if (cnOrAmp == "amp") {//忽略
return View("~/Web/Home/Amp/Index.cshtml",vm);//忽略
}//忽略
return View("~/Web/Home/Index.cshtml", vm);
}

这样就实现了model binding

最后就是主角了,validation。

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.2.0.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.16.0/jquery.validate.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validation.unobtrusive/3.2.6/jquery.validate.unobtrusive.min.js"></script>

  

就这样就有一个完整的form了,在asp.net Core MVC有个麻烦的问题,就是form不好管理,如果一个项目一个页面就只有一个form,那么没事发生,但是如果在多个页面重复那该怎么办,答案是ajaxing(MVC不支持)

介绍github 4000多个星星的plugin

https://github.com/jquery-form/form

<script>
// wait for the DOM to be loaded
$(document).ready(function () {
// bind 'myForm' and provide a simple callback function
$('.myForm').ajaxForm({
success: function (response, textStatus, xhr, form) {
console.log("in ajaxForm success"); if (!response.length || response != 'good') {
console.log("bad or empty response");
return xhr.abort();
}
console.log("All good. Do stuff");
},
error: function (xhr, textStatus, errorThrown) {
console.log("in ajaxForm error");
},
complete: function (xhr, textStatus) {
console.log("in ajaxForm complete");
},
beforeSend: function () {
console.log("before send");
}
});
});
</script>

对提交过程中处理不同的状况

以上

  

asp.net Core MVC + form validation + ajax form 笔记的更多相关文章

  1. asp net core mvc 跨域ajax解决方案

    1.配置服务端 在Startup文件中国配置Cors策略: IEnumerable<Client> clients= Configuration.GetSection("Clie ...

  2. ASP.NET Core MVC – Form Tag Helpers

    ASP.NET Core Tag Helpers系列目录 ASP.NET Core MVC Tag Helpers 介绍 ASP.NET Core MVC – Caching Tag Helpers ...

  3. ASP.NET Core 中文文档 第四章 MVC(01)ASP.NET Core MVC 概览

    原文:Overview of ASP.NET Core MVC 作者:Steve Smith 翻译:张海龙(jiechen) 校对:高嵩 ASP.NET Core MVC 是使用模型-视图-控制器(M ...

  4. ASP.NET Core MVC 之模型(Model)

    1.模型绑定 ASP.NET Core MVC 中的模型绑定将数据从HTTP请求映射到操作方法参数.参数既可以是简单类型,也可以是复杂类型.MVC 通过抽象绑定解决了这个问题. 2.使用模型绑定 当 ...

  5. ASP.NET Core MVC/WebAPi 模型绑定探索

    前言 相信一直关注我的园友都知道,我写的博文都没有特别枯燥理论性的东西,主要是当每开启一门新的技术之旅时,刚开始就直接去看底层实现原理,第一会感觉索然无味,第二也不明白到底为何要这样做,所以只有当你用 ...

  6. 创建ASP.NET Core MVC应用程序(4)-添加CRUD动作方法和视图

    创建ASP.NET Core MVC应用程序(4)-添加CRUD动作方法和视图 创建CRUD动作方法及视图 参照VS自带的基架(Scaffold)系统-MVC Controller with view ...

  7. ASP.NET Core MVC上传、导入、导出知多少

    前言 本君已成夜猫子,本节我们来讲讲ASP.NET Core MVC中的上传,这两天才研究批量导入功能,本节顺便简单搞搞导入.导出,等博主弄妥当了再来和大家一并分享. .NET Core MVC上传 ...

  8. 008.Adding a model to an ASP.NET Core MVC app --【在 asp.net core mvc 中添加一个model (模型)】

    Adding a model to an ASP.NET Core MVC app在 asp.net core mvc 中添加一个model (模型)2017-3-30 8 分钟阅读时长 本文内容1. ...

  9. ASP.NET Core Razor中处理Ajax请求

    如何ASP.NET Core Razor中处理Ajax请求 在ASP.NET Core Razor(以下简称Razor)刚出来的时候,看了一下官方的文档,一直没怎么用过.今天闲来无事,准备用Rozor ...

随机推荐

  1. linux lvs

  2. c#及js实现将金融变成3位一逗号

    1.c#用string.format ToString("#,###.00") 2.js方法 转自http://www.cnblogs.com/cssfirefly/p/35820 ...

  3. Java线程基础知识(状态、共享与协作)

    1.基础概念 CPU核心数和线程数的关系 核心数:线程数=1:1 ;使用了超线程技术后---> 1:2 CPU时间片轮转机制 又称RR调度,会导致上下文切换 什么是进程和线程 进程:程序运行资源 ...

  4. linux常用命令:date 命令

    在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...

  5. linux服务器---代理认证

    代理认证 proxy代理服务被广泛的使用,为了安全起见,可以在服务器上增加一层安全认证机制.这里使用htpasswd建立认证账号和密码 1.创建认证账号和密码 [root@localhost wj]# ...

  6. 设置(更改)Mysql 自增ID的起始值

    SELECT * FROM segwords WHERE id>790511 DELETE FROM segwords WHERE id>790511 #下面这句是设置的 ALTER TA ...

  7. python基础七--集合

    12.221.昨日内容回顾 小数据池: int:-5--256 str:1.不能有特殊字符 2.*int不能超过20 编码:所能看到的最小构成单位叫字符 ascii : 8位 1字节 表示1个字符 u ...

  8. Java并发之FairSync和NonfairSync

    Java并发中的fairSync和NonfairSync主要区别为: 如果当前线程不是锁的占有者,则NonfairSync并不判断是否有等待队列,直接使用compareAndSwap去进行锁的占用; ...

  9. 07:urllib与urllib2基本使用

    参考博客:https://blog.csdn.net/chendong_/article/details/51973499 1.1 urllib2发送get请求 # -*- coding:UTF-8 ...

  10. 小测(noip2005的两道题) 2017.3.3

    过河 题目描述 Description 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把 ...