mvc之验证IEnumerable<T> 类型,多选框验证
原文:mvc之验证IEnumerable<T> 类型,多选框验证
假设我们有这么一种需求,我们要同时添加年级和年级下面的多个班级,我们一般会像下面这种做法。
Action中我们这样接收:
[HttpPost]
public ActionResult CreateGrade(string gradeName, IEnumerable<string> classNames)
{ return View();
}
View中我们一般会这样做:
@using (Ajax.BeginForm("index", "home", new AjaxOptions { OnBegin="onBegin", OnSuccess = "onSuccess" }))
{
<p>
若要了解有关 ASP.NET MVC 的更多信息,请访问 @Html.ActionLink("link me", "about", "home", new { plugin="dialog"})。
</p>
<input type="text" class="required" style="width:90px;" name="gradeName" />
<input type="text" class="required" style="width:90px;" name="classNmae" />
<input type="text" class="required" style="width:90px;" name="classNmae" />
<input type="text" class="required" style="width:90px;" name="classNmae" />
<button class="tn-button-primary" type="submit">
<span class="tn-button-text">提交</span></button>
}
这种做法会有什么问题呢? 问题在于jquery.validate验证不支持验证多个相同的name,默认只验证第一个,所以只要第一个验证了,表单就可以提交了。我们要怎么改进呢?其实很简单,改一下班级的input的name就可以了。如下:
@using (Ajax.BeginForm("index", "home", new AjaxOptions { OnBegin="onBegin", OnSuccess = "onSuccess" }))
{
<p>
若要了解有关 ASP.NET MVC 的更多信息,请访问 @Html.ActionLink("link me", "about", "home", new { plugin="dialog"})。
</p>
<input type="text" class="required" style="width:90px;" name="gradeName" />
<input type="text" class="required" style="width:90px;" name="classNmae[0]" />
<input type="text" class="required" style="width:90px;" name="classNmae[1]" />
<input type="text" class="required" style="width:90px;" name="classNmae[2]" />
<button class="tn-button-primary " type="submit">
<span class="tn-button-text">提交</span></button>
}
这样子就可以每一个都验证了,类似这样子验证的还有IEnumerable<Grade>,可以这样子写grade.Name[0],grade.Name[1]。但是这样子还是有问题,就是我们只能通过class样式来验证,如必填项class="required"。改成这样之后我们要怎么实现通过类似$("form").validate({options})来配置验证呢? 不用急下面来介绍怎么实现吧。
@using (Ajax.BeginForm("index", "home", new AjaxOptions { OnBegin="onBegin", OnSuccess = "onSuccess" }))
{
<p>
若要了解有关 ASP.NET MVC 的更多信息,请访问 @Html.ActionLink("link me", "about", "home", new { plugin="dialog"})。
</p>
<input type="text" style="width:90px;" name="gradeName" />
<input type="text" style="width:90px;" name="classNmae[0]" class="classname" />
<input type="text" style="width:90px;" name="classNmae[1]" class="classname" />
<input type="text" style="width:90px;" name="classNmae[2]" class="classname" />
<button class="tn-button-primary " type="submit">
<span class="tn-button-text">提交</span></button>
}
<script type="text/javascript">
$(function () {
$("form").validate();//这句是必须的。
$("input.classname").each(function () {
$(this).rules("add", {
required: true,
number: true,
messages: {
required: "不能为空",
number: "只能是数字"
}
});
});
})
</script>
这样子就是现实了。
来一个完整的:
@using (Html.BeginForm("index", "home", FormMethod.Post, new { id="createForm"}))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>创建年级</legend>
<div class="editor-label">
年级名称
</div>
<div class="editor-field">
<input type="text" class="required" style="width:90px;" name="gradeName" />
</div>
<div class="editor-label">
班级1
</div>
<div class="editor-field">
<input type="text" style="width:90px;" name="classNmae[0]" class="classname" />
</div>
<div class="editor-label">
班级2
</div>
<div class="editor-field">
<input type="text" style="width:90px;" name="classNmae[1]" class="classname" />
</div>
<div class="editor-label">
班级3
</div>
<div class="editor-field">
<input type="text" style="width:90px;" name="classNmae[2]" class="classname" />
</div>
<p>
<button class="tn-button-primary " type="submit">
<span class="tn-button-text">提交</span></button>
</p>
</fieldset>
}
<script type="text/javascript">
$(function () {
$("#createForm").validate();//这句是必须的。
$("input.classname").each(function () {
$(this).rules("add", {
required: true,
number: true,
messages: {
required: "不能为空",
number: "只能是数字"
}
});
});
})
</script>
多选框验证:
<div class="tnc-select-checkbox tn-helper-clearfix">
@if (classes != null && classes.Count() > 0)
{
foreach (var item in classes)
{
<div class="tn-form-row">
@Html.SipmleCheckBox("classIds", item.Id, htmlAttributes: new { @class = "tn-radiobutton", id = "classId_" + item.Id })
<label for="@(string.Format("classId_{0}", item.Id))" title="@item.ClassFullName">
@StringUtility.Trim(item.ClassFullName, 7)</label>
</div>
}
}
</div>
$(function () {
$("#editForm").validate();
$("input[name='classIds']").rules("add", {
required: true,
messages: {
required: function () { alert("请至少选择一个班级。") }
}
});
});
mvc之验证IEnumerable<T> 类型,多选框验证的更多相关文章
- MVC验证11-对复杂类型使用jQuery异步验证
原文:MVC验证11-对复杂类型使用jQuery异步验证 本篇体验使用"jQuery结合Html.BeginForm()"对复杂类型属性进行异步验证.与本篇相关的"兄弟篇 ...
- MVC树控件,mvc中应用treeview,实现复选框树的多层级表单控件
类似于多层级的角色与权限控制功能,用MVC实现MVC树控件,mvc中应用treeview,实现复选框树的多层级表单控件.最近我们的项目中需要用到树型菜单,以前使用WebForm时,树型菜单有微软提供的 ...
- mvc之验证IEnumerable<T> 类型
假设我们有这么一种需求,我们要同时添加年级和年级下面的多个班级,我们一般会像下面这种做法. Action中我们这样接收: [HttpPost] public ActionResult CreateGr ...
- Spring MVC复选框
以下示例显示如何在使用Spring Web MVC框架的表单中使用复选框(Checkbox).首先使用Eclipse IDE来创建一个WEB工程,并按照以下步骤使用Spring Web Framewo ...
- Syncfusion 复选框 ComboBoxAdv
XAML: <syncfusion:GridTemplateColumn.EditTemplate> <DataTemplate DataType="viewModel:C ...
- Asp.net MVC验证那些事(4)-- 自定义验证特性
在项目的实际使用中,MVC默认提供的Validation Attribute往往不够用,难以应付现实中复杂多变的验证需求.比如, 在注册用户的过程中,往往需要用户勾选”免责声明”,这个checkbox ...
- Asp.net MVC验证哪些事(3)-- Remote验证及其改进(附源码)
表单中的输入项,有些是固定的,不变的验证规则,比如字符长度,必填等.但有些是动态的,比如注册用户名是否存在这样的检查,这个需要访问服务器后台才能解决.这篇文章将会介绍MVC中如何使用[RemoteAt ...
- ASP.NET MVC – 关于Action返回结果类型的事儿(上)
原文:ASP.NET MVC – 关于Action返回结果类型的事儿(上) 本文转自:博客园-文超的技术博客 一. ASP.NET MVC 1.0 Result 几何? Action的 ...
- 不存在具有键“xxxId”的“IEnumerable<SelectListItem>”类型的 ViewData 项
项目中的某个页面,在访问时出现以下错误: 不存在具有键“xxxId”的“IEnumerable<SelectListItem>”类型的 ViewData 项 具体的场景说明如下: 一个编辑 ...
随机推荐
- 【转】介绍Jython,第一部分:轻轻松松写JAVA程序
本文转自:http://www.ibm.com/developerworks/cn/education/java/j-jython1/index.html 关于本教程 本教程介绍哪些内容? 这个两部分 ...
- W5500EVB TCP Client模式设置说明
W5500EVB是WIZnet为了方便用户更好了解.使用W5500这款网络芯片所开发的评估板,该板採用了 STM32F103RCT6+W5500 的设计.基于 ARM 的 Cortex-M3 平台.那 ...
- DEMO阶段已完成,今天,要深入钻
今天老师整理我的代码,发现,当时我没搞清楚这是正常的,由于我没有在一开始发挥到其翻译,而没有分析. 只要,研究底部是正确的.为了更好地理解代码. 上午:OSGEARTH视频教程. 上午,DX11机械仿 ...
- SqlServer 添加列并赋值
有个需求,需要给某张表添加一列并且赋值,分解需求,一共分两部走: 添加列 赋值 两个功能都不难,很快实现. --add column alter table Med_Summary_Template ...
- role 'PLUSTRACE' does not exist
I have created a new user named watson and granted the related priviledges as following: SQL> cre ...
- MEF初体验之十一:查询组合容器
查询组合容器 组合容器暴露了几个get exports的重载方法和导出对象和对象集合.你需要注意下面的行为: 当请求单个对象实例时,如果未发现导出,一个异常将被抛出 当请求单个对象实例时,如果发现超过 ...
- NET MVC权限验证
ASP.NET MVC权限验证 封装类 写该权限类主要目地 为了让权限配置更加的灵活,可以根据SQL.json.或者XML的方式来动态进行页面的访问控制,以及没有权限的相关跳转. 使用步骤 1.要建一 ...
- iOS如何兼容的应用程序32位系统和64Bit系统
苹果发布iPhone5S时刻,64应用程序位去了眼前.当时我看到苹果公布的官方数据iOS7.x的SDK支撑64位应用程序.而内置的应用程序已经64位置. 我记得自己刚刚接触电脑时还有16位的系统,指针 ...
- UVa 10397 Connect the Campus
最小生成树 Kruskal #include<cmath> #include<iostream> #include<cstdio> #include<algo ...
- 每天一个JavaScript实例-canvas绘图
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...