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系统——DHCP

    DHCP定义DHCP服务是负责IP.掩码.网关地址.DNS地址等自动分发的软件服务DHCP的分配方式(1)自动分配:分配到一个IP地址后永久使用(2)手动分配:由DHCP服务器管理员专门指定IP地址( ...

  2. ts实战项目启动中遇到的问题

    项目链接:https://github.com/Jiasm/typescript-example 储备知识须知: sequelize入门篇 : 依照README执行以下操作: npm i brew s ...

  3. 5Lambda表达式

    C++11中的Lambda表达式用于定义并创建匿名的函数对象,以简化编程工作.首先看一下Lambda表达式的基本构成: [函数对象参数](操作符重载函数参数)mutable或exception -&g ...

  4. Maximum execution time of 30 seconds exceeded解决错误方法

    Maximum execution time of 30 seconds exceeded解决错误方法Fatal error: Maximum execution time of 30 seconds ...

  5. jenkin环境搭建

      Jenkins是一个用Java编写的开源的持续集成(CI)工具,可持续.自动地构建/测试软件项目,监控一些定时执行的任务.具有开源,支持多平台和插件扩展,安装简单,界面化管理等特点. 1.下载并解 ...

  6. python之路----socketserver模块

    socketserver import socketserver class MyServer(socketserver.BaseRequestHandler): def handle(self): ...

  7. nginx配置https并强制http自动跳转到https

    关于使用HTTPS/SSL的必要性,可以自行baidu,援引的说法,EFF(Electronic Frontier Foundation),全球过半流量采用https. https://www.osc ...

  8. 在Visual C#中使用XML指南之读取XML

    网站:http://www.yesky.com/155/1915155all.shtml#p1915155  

  9. Python跨平台打包

    对于pyinstaller,可以完成在windows,linux,和mac下的python脚本编译,生成exe,elf,.app文件: 1.使用方法: 在pyinstaller的官网上下载,一般是源码 ...

  10. ubuntu下交叉编译ffmpeg

    环境:ubuntu16.04 交叉编译器版本:4.8.3 依赖x264,lame x264: 1.wget ftp://ftp.videolan.org/pub/x264/snapshots/last ...