功能需求描述

Q:在实际的开发中,经常会遇到一个模型中包含有多个条目的表单。如何将数据提交到后台?

A: 以数组的形式提交到后台就Ok了(真的那么简单么,如果再嵌套一层呢?)

A2:拆分多个模型,映射就没啥问题了。但......有点麻烦啊~~

接下来说说如何将下面的模型提交到后台

    /// <summary>
/// 计划模型
/// </summary>
public class PlanModel
{
public int Id{ get; set; }
/// <summary>
/// 计划名称
/// </summary>
public string PlanName { get; set; }
/// <summary>
/// 描述
/// </summary>
public string Remark { get; set; }
/// <summary>
/// 方案集合
/// </summary>
public List<CaseModel> Cases { get; set; }
}
/// <summary>
/// 方案模型
/// </summary>
public class CaseModel
{
public int Id{ get; set; }
/// <summary>
/// 标题
/// </summary>
public string Title { get; set; }
/// <summary>
/// 描述
/// </summary>
public string Description { get; set; }
/// <summary>
/// 作者
/// </summary>
public string Author { get; set; } }

根据此模型,编辑的页面会如下图所示,一些基本信息加上可增可减的条目

实现效果

如何实现这个功能(asp.net mvc)

  1. 新建视图页面(略)
  2. 条目的显示增加删除

控制器代码

    public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
var model = new PlanModel() {};
return View(model);
}
public ActionResult CaseRow()
{
return View("_CaseRow", new CaseModel());
}
[HttpPost]
public ActionResult Form(PlanModel model)
{
return Json(model);
} }

编辑页条目显示代码

 <div class="form-group">
<label class="col-sm-3 control-label">计划方案:</label>
<div class="col-sm-7 ">
<table class="table table-bordered table-condensed">
<thead>
<tr class="text-center">
<th class="text-center">方案名称</th>
<th class="text-center">方案作者</th>
<th class="text-left">方案描述</th>
<th class="text-center" width="100">
<span>操作</span>
<span title="添加方案" id="add_case" class="glyphicon glyphicon-plus"></span>
</th>
</tr>
</thead>
<tbody id="case_list">
@if (Model.Cases != null)
{
foreach (var item in Model.Cases)
{
Html.RenderPartial("_CaseRow", item);
}
}
</tbody>
</table>
</div>
</div>

页面增加/删按钮js代码 + 验证

<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script type="text/javascript">
$(function () {
$("#case_list").delegate(".del_tr", "click", function () {
$(this).closest("tr").remove();
});
$("#add_case").click(function () {
//ajax请求返回新增方案视图代码
$.get('@Url.Action("CaseRow")', function (data) {
$("#case_list").append(data);
//重置验证模型
$("form").removeData("validator").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse($("form"));
});
}); });
</script>

_CaseRow.cshtml分部视图代码

若要以集合/数组的形式提交到后台,须以name[]的格式提交,所以我能想到的就是这样去写(这种方案不可取!!)

但是这样写的话且不说太麻烦,验证也不行,一不小心也就写错了。所以这种方案并不可取

@{
Layout = null;
KeyValuePair<string, string> keyValuePair = new KeyValuePair<string, string>("Cases", Guid.NewGuid().ToString("N"));
var prefix = keyValuePair.Key+"["+keyValuePair.Value+"].";
}
@model MvcDemo.Models.CaseModel
<tr>
<td>
<input type="hidden" name="@(keyValuePair.Key+".index")" value="@keyValuePair.Value"/>
<input type="hidden" class="form-control" name="@(prefix)Id" value="@Model.Id" />
<input type="text" class="form-control" name="@(prefix)Title" value="@Model.Title" />
</td>
<td>
@Html.TextBox(prefix+nameof(Model.Author),Model.Author, new { @class = "form-control" })
</td>
<td>
@Html.TextBox(prefix + nameof(Model.Description), Model.Description, new { @class = "form-control" })
</td>
<td class="text-center">
<span class="del_tr glyphicon glyphicon-remove-circle"></span>
</td>
</tr>

而后发现大神写的一个HtmlPrefixScopeExtensions扩展类,可自动生成的表单前缀标识,使用方便,也能够使用验证

只需将表单包裹在@using (Html.BeginCollectionItem("子集合的属性名称")){}中即可,文末分享

@{
Layout = null;
}
@model MvcDemo.Models.CaseModel
@using MvcDemo.Extensions
<tr>
@using (Html.BeginCollectionItem("Cases"))
{
<td>
@Html.HiddenFor(e => e.Id)
@Html.TextBoxFor(e => e.Title, new { @class = "form-control" })
@Html.ValidationMessageFor(e => e.Title)
</td>
<td>
@Html.TextBoxFor(e => e.Author, new { @class = "form-control" })
</td>
<td>
@Html.TextBoxFor(e => e.Description, new { @class = "form-control" })
</td>
<td class="text-center">
<span class="del_tr glyphicon glyphicon-remove-circle"></span>
</td>
}
</tr>

然后提交表单可以发现格式如下,并能取到数据

MvcDemo.Extensions命名空间下的HtmlPrefixScopeExtensions扩展类

命名空间自行引用

  1. asp.net mvc版本
    public static class HtmlPrefixScopeExtensions
{
private const string IdsToReuseKey = "__htmlPrefixScopeExtensions_IdsToReuse_"; /// <summary>
///
/// </summary>
/// <param name="html"></param>
/// <param name="collectionName"></param>
/// <param name="createDummyForm">是否使用虚拟表单,为了解决上下文中不存在表单,无法生成验证信息</param>
/// <returns></returns>
public static IDisposable BeginCollectionItem(this HtmlHelper html, string collectionName,
bool createDummyForm = false, bool clientValidationEnabled = false)
{
if (clientValidationEnabled == true)
html.ViewContext.ClientValidationEnabled = true; if (createDummyForm == true)
{
if (html.ViewContext != null && html.ViewContext.FormContext == null)
{
var dummyFormContext = new FormContext();
html.ViewContext.FormContext = dummyFormContext;
}
} return BeginCollectionItem(html, collectionName, html.ViewContext.Writer);
} private static IDisposable BeginCollectionItem(this HtmlHelper html, string collectionName, TextWriter writer)
{
var idsToReuse = GetIdsToReuse(html.ViewContext.HttpContext, collectionName);
var itemIndex = idsToReuse.Count > 0 ? idsToReuse.Dequeue() : Guid.NewGuid().GetHashCode().ToString("x"); writer.WriteLine(
"<input type=\"hidden\" name=\"{0}.index\" autocomplete=\"off\" value=\"{1}\" />",
collectionName, html.Encode(itemIndex)); return BeginHtmlFieldPrefixScope(html, string.Format("{0}[{1}]", collectionName, itemIndex));
} private static IDisposable BeginHtmlFieldPrefixScope(this HtmlHelper html, string htmlFieldPrefix)
{
return new HtmlFieldPrefixScope(html.ViewData.TemplateInfo, htmlFieldPrefix);
} private static Queue<string> GetIdsToReuse(HttpContextBase httpContext, string collectionName)
{
var key = IdsToReuseKey + collectionName;
var queue = (Queue<string>)httpContext.Items[key];
if (queue == null)
{
httpContext.Items[key] = queue = new Queue<string>();
var previouslyUsedIds = httpContext.Request[collectionName + ".index"];
if (!string.IsNullOrEmpty(previouslyUsedIds))
foreach (var previouslyUsedId in previouslyUsedIds.Split(','))
queue.Enqueue(previouslyUsedId);
}
return queue;
} internal class HtmlFieldPrefixScope : IDisposable
{
internal readonly TemplateInfo TemplateInfo;
internal readonly string PreviousHtmlFieldPrefix; public HtmlFieldPrefixScope(TemplateInfo templateInfo, string htmlFieldPrefix)
{
TemplateInfo = templateInfo; PreviousHtmlFieldPrefix = TemplateInfo.HtmlFieldPrefix;
TemplateInfo.HtmlFieldPrefix = htmlFieldPrefix; } public void Dispose()
{
TemplateInfo.HtmlFieldPrefix = PreviousHtmlFieldPrefix;
}
}
}
  1. asp.net core版本
    public static class HtmlPrefixScopeExtensions
{
private const string IdsToReuseKey = "__htmlPrefixScopeExtensions_IdsToReuse_"; public static IDisposable BeginCollectionItem(this IHtmlHelper html, string collectionName)
{
return BeginCollectionItem(html, collectionName, html.ViewContext.Writer);
} private static IDisposable BeginCollectionItem(this IHtmlHelper html, string collectionName, TextWriter writer)
{
if (html.ViewData["ContainerPrefix"] != null)
collectionName = string.Concat(html.ViewData["ContainerPrefix"], ".", collectionName); var idsToReuse = GetIdsToReuse(html.ViewContext.HttpContext, collectionName);
var itemIndex = idsToReuse.Count > 0 ? idsToReuse.Dequeue() : Guid.NewGuid().ToString(); string htmlFieldPrefix = $"{collectionName}[{itemIndex}]";
html.ViewData["ContainerPrefix"] = htmlFieldPrefix; /*
* html.Name(); has been removed
* because of incorrect naming of collection items
* e.g.
* let collectionName = "Collection"
* the first item's name was Collection[0].Collection[<GUID>]
* instead of Collection[<GUID>]
*/
string indexInputName = $"{collectionName}.index"; // autocomplete="off" is needed to work around a very annoying Chrome behaviour
// whereby it reuses old values after the user clicks "Back", which causes the
// xyz.index and xyz[...] values to get out of sync.
writer.WriteLine($@"<input type=""hidden"" name=""{indexInputName}"" autocomplete=""off"" value=""{html.Encode(itemIndex)}"" />"); return BeginHtmlFieldPrefixScope(html, htmlFieldPrefix);
} private static IDisposable BeginHtmlFieldPrefixScope(this IHtmlHelper html, string htmlFieldPrefix)
{
return new HtmlFieldPrefixScope(html.ViewData.TemplateInfo, htmlFieldPrefix);
} private static Queue<string> GetIdsToReuse(HttpContext httpContext, string collectionName)
{
// We need to use the same sequence of IDs following a server-side validation failure,
// otherwise the framework won't render the validation error messages next to each item.
var key = IdsToReuseKey + collectionName;
var queue = (Queue<string>)httpContext.Items[key];
if (queue == null)
{
httpContext.Items[key] = queue = new Queue<string>(); if (httpContext.Request.Method == "POST" && httpContext.Request.HasFormContentType)
{
StringValues previouslyUsedIds = httpContext.Request.Form[collectionName + ".index"];
if (!string.IsNullOrEmpty(previouslyUsedIds))
foreach (var previouslyUsedId in previouslyUsedIds)
queue.Enqueue(previouslyUsedId);
}
}
return queue;
} internal class HtmlFieldPrefixScope : IDisposable
{
internal readonly TemplateInfo TemplateInfo;
internal readonly string PreviousHtmlFieldPrefix; public HtmlFieldPrefixScope(TemplateInfo templateInfo, string htmlFieldPrefix)
{
TemplateInfo = templateInfo; PreviousHtmlFieldPrefix = TemplateInfo.HtmlFieldPrefix;
TemplateInfo.HtmlFieldPrefix = htmlFieldPrefix;
} public void Dispose()
{
TemplateInfo.HtmlFieldPrefix = PreviousHtmlFieldPrefix;
}
}
}

命名空间自行引用~~

End

完整源码:https://coding.net/u/yimocoding/p/WeDemo/git/tree/MvcFormExt/MvcFormExt/MvcDemo

mvc一对多模型表单的快速构建的更多相关文章

  1. django模型表单ModelForm

    如果你正在构建一个数据库驱动的应用,那么你可能会有与Django的模型紧密映射的表单.比如,你有个BlogComment模型,并且你还想创建一个表单让大家提交评论到这个模型中.在这种情况下,写一个fo ...

  2. MVC动态生成的表单:表单元素比较多 你就这样写

    MVC动态生成的表单:表单元素比较多 你就这样写: public ActionResult ShoudaanActionResult(FormCollection from,T_UserM user) ...

  3. 模型表单ModleForm

    官方文档网址   http://python.usyiyi.cn/documents/django_182/topics/forms/modelforms.html 模型表单的应用场景 如果你正在构建 ...

  4. 第四章:Django表单 - 5:模型表单ModelForm

    如果你正在构建一个数据库驱动的应用,那么你可能会有与Django的模型紧密映射的表单.比如,你有个BlogComment模型,并且你还想创建一个表单让大家提交评论到这个模型中.在这种情况下,写一个fo ...

  5. Asp.net Mvc Ajax.BeginForm提交表单

    之前Mvc中一直用Html.BeginForm提交表单,即如下: @using (Html.BeginForm("Add", "News", FormMetho ...

  6. 如何在.Net Core MVC中为动态表单开启客户端验证

    非Core中的请参照: MVC的验证 jquery.validate.unobtrusive mvc验证jquery.unobtrusive-ajax 参照向动态表单增加验证 页面引入相关JS: &l ...

  7. [Swift通天遁地]二、表格表单-(9)快速创建一个美观强大的表单

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. [Swift通天遁地]二、表格表单-(8)快速实现表单的输入验证

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  9. spring mvc 防止重复提交表单的两种方法,推荐第二种

    第一种方法:判断session中保存的token 比较麻烦,每次在提交表单时都必须传入上次的token.而且当一个页面使用ajax时,多个表单提交就会有问题. 注解Token代码: package c ...

随机推荐

  1. 使用webpack-dev-middleware 和 webpack-hot-middleware 配置一个dev-server

    关于Webpack的资料教程网上已经数不胜数,但是对手动配置一个Express server的确不多,于是我对此进行着重的了解一番. webpack-dev-middleware和webpack-ho ...

  2. C语言:min和max头文件

    转自:http://www.cppblog.com/jince/archive/2010/09/14/126600.html min和max头文件 虽然说求最大值最小值函数在哪个头文件下并不是非常重要 ...

  3. MongoDb 入门教程

    MongoDb 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. 它是可扩展的高性能数据存储解决方案,经常被用于非关系型数据的存储,能存储海量的数据. 常 ...

  4. JavaScript中事件

    JS中的事件 一.事件分类: 鼠标事件:鼠标单击.鼠标双击.鼠标指上等... HTML事件:文档加载.焦点.表单提交等... 键盘事件:键盘按下(keydown).键盘按下并松开瞬间(keypress ...

  5. apache如何设置缓存

    基本介绍 httpd是一个比较经典的web服务器,也就是静态资源服务器,主要用来服务于一些静态的文件,例如css,js,html等文件,所谓的静态文件,也就是不需要通过服务器进行运行的文件. 在使用静 ...

  6. 比较两个date返回日期相差天数

    public static int daydiff(Date fDate, Date oDate) { Calendar aCalendar = Calendar.getInstance(); aCa ...

  7. 微信小程序开发历程

    小程序:    帮助文档:(https://mp.weixin.qq.com/debug/wxadoc/dev/index.html) 优势:    一种无须安装即可运行的程序,与订阅号,服务号是同一 ...

  8. Ubuntu操作系统下安装JDK、tomcat、mysql

    1.先从安装虚拟机开始 01.首先打开VMware虚拟机.     02.然后,进入home主页,点击"create a New Virtual Machine"一栏,就会弹出一个 ...

  9. 转:【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/17225469    在Java中,可以通过配合调用Object对象的wait()方法和no ...

  10. 【Beta阶段】第四次scrum meeting

    Coding/OSChina 地址 1. 会议内容 学号 主要负责的方向 昨日任务 昨日任务完成进度 接下去要做 99 PM 查找适合的素材模块,和105一起把手势功能连接到APP上 100% 查阅换 ...