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. Intermediate Python for Data Science learning 2 - Histograms

    Histograms from:https://campus.datacamp.com/courses/intermediate-python-for-data-science/matplotlib? ...

  2. 使用IP连接SQL SERVER或者配置为连接字符串失败

    使用IP连接SQL SERVER或者配置为连接字符串失败 情景一:当在webconfig文件中使用   <add key="ConnectionString" value=& ...

  3. linux常用命令:free 命令

    free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及被内核使用的buffer.在Linux系统监控的工具中,free命令是最经常使用的命令之一. 1.命令格式: free [参 ...

  4. 20165207 Exp4 恶意代码分析

    目录 1.实验内容 1.1.系统运行监控 1.1.1.使用命令行创建计划任务 1.1.2.使用命令行借助批处理文件创建计划任务 1.1.3.分析netstat计划任务的最终结果 1.1.4.安装配置s ...

  5. Linux基础命令---resizefs

    resize2fs 调整ext2\ext3\ext4文件系统的大小,它可以放大或者缩小没有挂载的文件系统的大小.如果文件系统已经挂载,它可以扩大文件系统的大小,前提是内核支持在线调整大小. size参 ...

  6. c++第十四天

    <c++ primer, 5E> 第91页到第94页,笔记: 1.vector支持的操作. v.empty().v.size().v.push_back(t).v[n] 2.试图通过下标访 ...

  7. [Java]接受拖拽文件的窗口

    至于这个问题,Java的awt.dnd包下提供了许多完成这一功能的类 例如DropTarget.DropTargetListener等 先来讲一下DropTarget类,这个类完成和拖拽.复制文件等操 ...

  8. Python3基础 super 子类调用父类的__init__

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  9. Linux内核分析--内核中的数据结构双向链表续【转】

    在解释完内核中的链表基本知识以后,下面解释链表的重要接口操作: 1. 声明和初始化 实际上Linux只定义了链表节点,并没有专门定义链表头,那么一个链表结构是如何建立起来的呢?让我们来看看LIST_H ...

  10. 分布式系统一致性协议--Paxos算法

    Paxos: Paxos算法背景介绍: Paxos算法是分布式技术大师Lamport提出的,主要目的是通过这个算法,让参与分布式处理的每个参与者逐步达成一致意见.用好理解的方式来说,就是在一个选举过程 ...