AspNetMvc 防止重复提交
2015-05-21 16:30
351 查看
表单提交时需要校验数据是否已存在,如果已存在需要防止重复提交,做法比较简单,不再赘述。
1.Employee类
1.Employee类
public class Employee { public int Id { set; get; } [StringLength(10,MinimumLength=10)] public string Name { set; get; } [RegularExpression("^[1-9][0-9]{0,1}$")] public Nullable<int> Age { set; get; } }2.View
@model AspNetMvcModelValidate.Models.Employee <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#btn").click(function () { var name = $('#name').val(); $.post('@Url.Action("CheckEmployeeName")', { name: name }, function (data) { console.log(data); if (data) { console.log("yes"); $("#myform").submit(); } else { alert("Employee with name:" + name + "already exist.");//已有相同 } }) }); }); </script> <h2>Index2</h2> @using (@Html.BeginForm("AddEmployee", "Home", FormMethod.Post, new { Id = "myform" })) { <table> <tr> <td>Name</td> <td>@Html.TextBoxFor(s => s.Name, new { ID = "name" })</td> </tr> <tr> <td>Age</td> <td>@Html.TextBoxFor(s => s.Age)</td> </tr> <tr> <td colspan="2"><input type="button" id="btn" value="submit" style="width:215px" /></td> </tr> </table> }3.控制器
public JsonResult CheckEmployeeName(string name) { var query = ModelsContainer.Employees.Where(x => x.Name == name).Count<Employee>(); return Json(query < 1, JsonRequestBehavior.AllowGet); } public ActionResult AddEmployee(Employee employee) { ModelsContainer.Employees.Add(employee); return View(employee); }4.数据源
public class ModelsContainer { public static List<Employee> Employees = new List<Employee> { new Employee{ Id = 1, Name = "Employee01", Age = 25 }, new Employee{ Id = 2, Name = "Employee02", Age = 25 } }; }5.Result
相关文章推荐
- Asp.net MVC中防止HttpPost重复提交
- Asp.net MVC中防止HttpPost重复提交
- 在asp.net mvc中使用ActionFilter防止重复提交
- Asp.net MVC中防止HttpPost重复提交
- ASP.NET Web Form和MVC中防止F5刷新引起的重复提交问题
- asp.net mvc 防止重复提交
- 在ASP.NET Web Form和MVC中防止F5刷新引起的重复提交
- Asp.net MVC中防止HttpPost重复提交
- Asp.net MVC中防止HttpPost重复提交
- Asp.net MVC中防止HttpPost重复提交
- asp.net禁用按钮以防止重复提交
- asp.net页面防止按钮重复提交的小技巧
- Asp.net实现弹出窗口提示,又防止刷新被重复提交的方法
- Asp.net实现弹出窗口提示,又防止刷新被重复提交的方法
- ASP.net防止用户后退、刷新造成重复提交的方法研究
- ASP.Net中防止页面刷新重复提交的几种方法
- asp.net防止页面重复提交
- ASP.Net防止页面刷新重复提交
- ASP.NET中防止刷新页面造成表单重复提交
- Asp.Net防止刷新重复提交数据的办法