Model Validation 和测试Post参数
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcModelApp
{ [MetadataType(typeof(UserMetadata))]
public partial class tb_User
{
public string RePwd { get; set; }
} public class UserMetadata
{
[DisplayName("用户名")]
[Remote("NotExitesUserName", "Home")]
public string UserName { get; set; }
[DisplayName("备注")]
[DataType(DataType.MultilineText)]
public string Remark { get; set; }
[DisplayName("年龄")]
[Range(, )]
public int Age { set; get; } [DisplayName("密码")]
[PasswordPropertyText]
public string Pwd { get; set; } [PasswordPropertyText]
[DisplayName("重述密码")]
[System.Web.Mvc.Compare("Pwd")]
public string RePwd { get; set; } [Email]
public string Email { get; set; }
} public class EmailAttribute : RegularExpressionAttribute
{
public EmailAttribute()
: base(@"^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$")
{ }
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
这个是ADO.NET EF 自动生成的类
namespace MvcModelApp
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc; public partial class tb_User
{
public int ID { get; set; }
public string UserName { get; set; }
public string Remark { get; set; }
public int Age { get; set; }
public string Pwd { get; set; }
public string Email { get; set; }
} }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcModelApp.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
private TestMvcEntities db = new TestMvcEntities();
public ActionResult Index()
{
var list = db.tb_User.ToList();
return View(list);
} public ActionResult Register()
{
tb_User model = new tb_User();
return View(model);
}
[HttpPost]
public ActionResult Register(tb_User model)
{
if (ModelState.IsValid)
{
db.tb_User.Add(model);
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
return View();
} }
[HttpGet]
public JsonResult NotExitesUserName()
{
string UserName = Request.Params["UserName"];
var user = db.tb_User.Where(c => c.UserName == UserName).FirstOrDefault();
return user == null ? Json(true, JsonRequestBehavior.AllowGet) : Json(false, JsonRequestBehavior.AllowGet);
} public JsonResult TestArr(List<string> arr)
{
//string ss=
return Json(new {success= });
} }
}
@model IEnumerable<MvcModelApp.tb_User>
@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.7.2.min.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<h2>Index</h2>
<script type="text/javascript">
$(document).ready(function () {
//$(":text").map(function () {
// alert($(this).val());
//})
var arr = new Array();
//var objstr = $(":text").map(function () {
// return $(this).text();
//}).get().join(",");
$("#btnTest").click(function () {
// $(":text").each(function () { alert($(this).val());});
$("input[id^='txt']").each(function () {
arr.push($(this).val());
// alert($(this).val());
});
// var jsonobj = {"arr":arr,"ljj":"1"};
$.ajax({
type: "POST",
url: "@Url.Action("TestArr", "Home")",
data: JSON.stringify(arr),
contentType: "application/json",
success: function (data, status) {
alert(data);
}
});
// var str = arr.join(",");
// alert(str);
});
});
// var obj =
</script>
<p>
<input type="text" id="txt1" />
<input type="text" id="txt2" />
<input type="text" id="txt3" />
<input type="text" id="txt4" />
<input id="btnTest" type="button" value="测试传递数组对象" />
</p>
<p>
@Html.ActionLink("Register","Register", "Home")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.RePwd)
</th>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
<th>
@Html.DisplayNameFor(model => model.Remark)
</th>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th>
@Html.DisplayNameFor(model => model.Pwd)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.RePwd)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Remark)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
<td>
@Html.DisplayFor(modelItem => item.Pwd)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
@model MvcModelApp.tb_User
@{
ViewBag.Title = "Register";
}
<script src="~/Scripts/jquery-1.7.2.min.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<h2>Register</h2>
@using (Html.BeginForm("Register","Home",null,FormMethod.Post,new {id="form1"}))
{
<table>
<tr>
<td>@Html.Display("UserName")</td>
<td>@Html.TextBoxFor(x=>x.UserName)@Html.ValidationMessageFor(x=>x.UserName)</td>
</tr>
<tr>
<td>@Html.DisplayFor(x=>x.Age)</td>
<td>@Html.TextBoxFor(x=>x.Age)@Html.ValidationMessageFor(x=>x.Age)</td>
</tr>
<tr>
<td>@Html.DisplayFor(x=>x.Pwd)</td>
<td>@Html.TextBoxFor(x=>x.Pwd)@Html.ValidationMessageFor(x=>x.Pwd)</td>
</tr>
<tr>
<td>@Html.DisplayFor(x=>x.RePwd)</td>
<td>@Html.TextBoxFor(x=>x.RePwd)@Html.ValidationMessageFor(x=>x.RePwd)</td>
</tr>
<tr>
<td>@Html.DisplayFor(x=>x.Email)</td>
<td>@Html.TextBoxFor(x=>x.Email)@Html.ValidationMessageFor(x=>x.Email)</td>
</tr>
<tr>
<td>@Html.DisplayFor(x=>x.Remark)</td>
<td>@Html.TextBoxFor(x=>x.Remark)@Html.ValidationMessageFor(x=>x.Remark)</td>
</tr>
<tr>
<td colspan=""><input type="submit" value="注册"/></td>
</tr>
</table>
}
Model Validation 和测试Post参数的更多相关文章
- 训练集(train set) 验证集(validation set) 测试集(test set)
转自:http://www.cnblogs.com/xfzhang/archive/2013/05/24/3096412.html 在有监督(supervise)的机器学习中,数据集常被分成2~3个, ...
- <转>ASP.NET学习笔记之MVC 3 数据验证 Model Validation 详解
MVC 3 数据验证 Model Validation 详解 再附加一些比较好的验证详解:(以下均为引用) 1.asp.net mvc3 的数据验证(一) - zhangkai2237 - 博客园 ...
- [机器学习] 训练集(train set) 验证集(validation set) 测试集(test set)
在有监督(supervise)的机器学习中,数据集常被分成2~3个即: 训练集(train set) 验证集(validation set) 测试集(test set) 一般需要将样本分成独立的三部分 ...
- AI---训练集(train set) 验证集(validation set) 测试集(test set)
在有监督(supervise)的机器学习中,数据集常被分成2~3个即: 训练集(train set) 验证集(validation set) 测试集(test set) 一般需要将样本分成独立的三部分 ...
- 训练集(train set) 验证集(validation set) 测试集(test set)。
训练集(train set) 验证集(validation set) 测试集(test set). http://blog.sina.com.cn/s/blog_4d2f6cf201000cjx.ht ...
- Model Validation in ASP.NET Web API
Model Validation in ASP.NET Web API 原文:http://www.asp.net/web-api/overview/formats-and-model-binding ...
- Model Validation in Asp.net MVC
原文:Model Validation in Asp.net MVC 本文用于记录Pro ASP.NET MVC 3 Framework中阐述的数据验证的方式. 先说服务器端的吧.最简单的一种方式自然 ...
- paip.提升性能---mysql 性能 测试以及 参数调整.txt
paip.提升性能---mysql 性能 测试以及 参数调整.txt 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://b ...
- [转载]SOAPUI压力测试的参数配置
原文地址:SOAPUI压力测试的参数配置作者:goooooodlife The different Load Strategies available in soapUI and soapUI Pro ...
随机推荐
- 【docker问题】Client.Timeout exceeded while awaiting headers
在进行docker pull 拉取镜像时,出现过下面的错误: net/http: request canceled while waiting for connection (Client.Timeo ...
- Codeforces Round #616 (Div. 2) D
莫队的模板 struct node{ int l,r,id; }q[maxn]; int cmp(node a,node b) { ) ? a.r < b.r : a.r > b.r); ...
- 安装插件报错error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++
起因:学到多线程greenlet模块时,greenlet模块死活装不上,以为pycharm坏掉了,浪费了一下午. #pip3 install greenlet from greenlet import ...
- Linux中制作静态库
静态库生成: 1.第一步:生成.o文件 2.第二步:将所有.o文件打包 ar src libMyTest.a *.o 生成出libMyTest.a 3.使用: 第一种:gcc main.c ...
- 在一个不稳定的无效的ViewState净的应用问题。 Erratic Invalid Viewstate issue in a .NET application
这似乎是很多人都经历了同样的IE8的问题.似乎发生的是,不知何故,IE8(在IE8的渲染模式和IE7兼容模式)将失去4096个字节的HTML文档中该数据缺失导致此异常(通常你看到这一scriptres ...
- centos 下安装nginx
安装make: yum -y install gcc automake autoconf libtool make 安装g++: yum install gcc gcc-c++ cd /usr/loc ...
- Practical aspects of deep learning
If your Neural Network model seems to have high variance, what of the following would be promising t ...
- 2019年mybatils面试高频题(java)
前前言 2019即将过去,伴随我们即将迎来的又是新的一年,过完春节,马上又要迎来新的金三银四面试季. 那么,作为程序猿的你,是否真的有所准备的呢,亦或是安于本职工作,继续做好手头上的事情. 当然,不论 ...
- shell脚本添加脚本执行时间和当前运行次数current running time
#!/bin/bash ############################ #Author:Bing #Create time:3/31/2017 ####################### ...
- STM32新MCU
G0的出现完美的替换自家目前的F0系列而且有更好的性能和价格优势; STM32WL世界上首款LoRa Soc单片机嵌入了基于Semtech SX126x的经过特殊设计的无线电,该无线电提供两种功率输出 ...