form 利用BeginCollectionItem提交集合List<T>数据 以及提交的集合中含有集合的数据类型 如List<List<T>> 数据的解决方案
例子:
public class IssArgs
{
public List<IssTabArgs> Tabs { get; set; }
} public class IssTabArgs
{
public int Num { get; set; } public string Tab { get; set; } public List<IssArg> Args { get; set; }
} public class IssArg
{
public int Num { get; set; } public string Name { get; set; } public string Value { get; set; }
}
现在需要上传这样的数据
@using (Html.BeginForm("SaveGpIss", "Issue"))
{
@foreach (var item in Model.TabArgs.Tabs)
{
@Html.Partial("_ArgsTabPar",item)
}
}
@model Web.Models.Vm.Issue.IssTabArgs
<tbody>
@using (Html.BeginCollectionItem("TabArgs.Tabs"))
{
@Html.HiddenFor(m => m.Num, new { @class = "arg_tab_num" })
<tr class="un">
<td colspan="" class="w">@Html.EditorFor(m=>m.Tab, new { HtmlAttributes = new { PlaceHolder = "表名称" } })</td>
</tr>
foreach (var item in Model.Args)
{
@Html.Partial("_ArgPar",item)
}
}
</tbody>
下面是第级集合的重点
@model Web.Models.Vm.Issue.IssArg
@{
var colId = ViewData.TemplateInfo.HtmlFieldPrefix;
}
<tr>
@using (Html.BeginCollectionItem(colId + ".Args"))
{
@Html.HiddenFor(m=>m.Num,new {@class="arg_num"})
<td class="n">@Html.EditorFor(m => m.Name, new { HtmlAttributes = new { PlaceHolder = "参数名称" } })</td>
<td class="i">@Html.EditorFor(m => m.Value, new { HtmlAttributes = new { PlaceHolder = "参数值" } })</td>
}
</tr>
ViewData.TemplateInfo.HtmlFieldPrefix;
BeginCollectionItem 反编译代码,将 Guid.NewGuid().ToString() 值储存 ViewData.TemplateInfo.HtmlFieldPrefix;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Mvc; namespace HtmlHelpers.BeginCollectionItem
{
public static class HtmlPrefixScopeExtensions
{
private const string IdsToReuseKey = "__htmlPrefixScopeExtensions_IdsToReuse_"; public static IDisposable BeginCollectionItem(this HtmlHelper html, string collectionName)
{
return html.BeginCollectionItem(collectionName, html.ViewContext.Writer);
} public static IDisposable BeginCollectionItem(this HtmlHelper html, string collectionName, TextWriter writer)
{
Queue<string> idsToReuse = HtmlPrefixScopeExtensions.GetIdsToReuse(html.ViewContext.get_HttpContext(), collectionName);
string str = idsToReuse.Count > ? idsToReuse.Dequeue() : Guid.NewGuid().ToString();
writer.WriteLine("<input type=\"hidden\" name=\"{0}.index\" autocomplete=\"off\" value=\"{1}\" />", (object) collectionName, (object) html.Encode(str));
return html.BeginHtmlFieldPrefixScope(string.Format("{0}[{1}]", (object) collectionName, (object) str));
} public static IDisposable BeginHtmlFieldPrefixScope(this HtmlHelper html, string htmlFieldPrefix)
{
return (IDisposable) new HtmlPrefixScopeExtensions.HtmlFieldPrefixScope(html.ViewData.TemplateInfo, htmlFieldPrefix);
} private static Queue<string> GetIdsToReuse(HttpContextBase httpContext, string collectionName)
{
string str1 = "__htmlPrefixScopeExtensions_IdsToReuse_" + collectionName;
Queue<string> stringQueue = (Queue<string>) httpContext.Items[(object) str1];
if (stringQueue == null)
{
httpContext.Items[(object) str1] = (object) (stringQueue = new Queue<string>());
string str2 = httpContext.Request[collectionName + ".index"];
if (!string.IsNullOrEmpty(str2))
{
string str3 = str2;
char[] chArray = new char[]{ ',' };
foreach (string str4 in str3.Split(chArray))
stringQueue.Enqueue(str4);
}
}
return stringQueue;
} internal class HtmlFieldPrefixScope : IDisposable
{
internal readonly TemplateInfo TemplateInfo;
internal readonly string PreviousHtmlFieldPrefix; public HtmlFieldPrefixScope(TemplateInfo templateInfo, string htmlFieldPrefix)
{
this.TemplateInfo = templateInfo;
this.PreviousHtmlFieldPrefix = this.TemplateInfo.HtmlFieldPrefix;
this.TemplateInfo.HtmlFieldPrefix = htmlFieldPrefix;
} public void Dispose()
{
this.TemplateInfo.HtmlFieldPrefix = this.PreviousHtmlFieldPrefix;
}
}
}
}
还有一种解决方式就是通过脚本的方式改变input name 的内容,这里不多说了
form 利用BeginCollectionItem提交集合List<T>数据 以及提交的集合中含有集合的数据类型 如List<List<T>> 数据的解决方案的更多相关文章
- C#中的集合
[集合不同于数组,是一组可变类型的.可变数量的元素的组合,这些元素可能共享某些特征,需要以某种操作方式一起进行操作.一般来讲,为了便于操作这些元素的类型是相同的] [集合与数组的区别:数组是连续的.同 ...
- ffmpeg从AVFrame取出yuv数据到保存到char*中
ffmpeg从AVFrame取出yuv数据到保存到char*中 很多人一直不知道怎么利用ffmpeg从AVFrame取出yuv数据到保存到char*中,下面代码将yuv420p和yuv422p的数 ...
- python 读取SQLServer数据插入到MongoDB数据库中
# -*- coding: utf-8 -*-import pyodbcimport osimport csvimport pymongofrom pymongo import ASCENDING, ...
- java中的集合和视图
一.集合的概念 何为集合,集合就是相当于一个对象的容器.集合是类似数组的一个作用.既然有了数组,为何还要有集合呢,由于数组对象一旦创建,其大小便不可以更改,我们只能往数组中存放创建时数量的对象.而集合 ...
- JAVA笔记整理(九),JAVA中的集合
在工作中,我们经常需要将多个对象集中存放,可以使用数组,但是数组的长度一旦固定之后是不可变的,为了保存数量确定的数据,我们可以使用JAVA中的集合. 在我看来,JAVA中的集合可以看作是一个特殊的数据 ...
- form表单提交数据,页面必定会刷新,ajax提交数据不会刷新,做到悄悄提交,多选删除,ajax提交实例
很多页面用到的模态对话框,如知明网站https://dig.chouti.com/的登录页都是模态对话框, 当点登录时,是用的ajax提交,因为输入错了信息,有返回消息,而页面没有刷新. jquery ...
- 使用jQuery.form插件,实现完美的表单异步提交
传送门:异步编程系列目录…… 时间真快,转眼一个月快结束了,一个月没写博客了!手开始生了,怎么开始呢…… 示例下载:使用jQuery.form插件,实现完美的表单异步提交.rar 月份的尾巴,今天的主 ...
- js表单动态添加数据并提交
情景1:已经存在form对象了,动态为form增加对象并提交 function formAppendSubmit(){ var myform=$('#newArticleForm'); //得到for ...
- Spring MVC防止数据重复提交
现实开发中表单重复提交的例子很多,就包括手上这个门户的项目也有这种应用场景,用的次数多,但是总结,这还是第一次. 一.基本原理 使用token,给所有的url加一个拦截器,在拦截器里面用java的UU ...
随机推荐
- mfc开发an unsupported operation was attempted错误解决
mfc开发删除了一个控件后,没有删除该控件对应的id和代码导致 觉得mfc真xx 在资源编辑可视化界面手动删除一个控件后,resource.h里该控件的ID竟然还存在 因为该id还存在,调用该控件的代 ...
- WPF WindowChrome 自定义窗口
1.wpf自定义窗口: WindowChrome类描述:https://msdn.microsoft.com/zh-cn/library/system.windows.shell.windowchro ...
- DELPHI XE2 采用 JSON 的方式来序列化对象
DELPHI XE2 采用 JSON 的方式来序列化对象 以下代码测试通过.问题是里面的中文,在反序列化后是乱码. 1. 序列化对象为字符串,Subject 里面的中文看起来正常,仍然是中文: 2. ...
- Ceph OpenSSL
Ceph OpenSSL 1. SSL介绍 SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信 ...
- uniConnection断线重联(tag属性颇有深意,这样就可以在某些情况下,不用继承实现新控件就可以达到自己的目的)
群友无法呼吸提供的,谢谢他了. http://blog.sina.com.cn/s/blog_44fa172f0102wb7h.html
- c#自定义业务锁
我们有这样的使用场景,某个订单在修改信息的时候,其他人不能修改相关的信息,比如不能做支付,不能退单等等,那么我们可以根据单号进行加锁,多Monitor做了如下扩展 定义接口 //// 文件名称:ILo ...
- 记一次在win2008下添加nginx自启动服务的操作
为了在win环境下添加nginx自启服务,我度娘了, 找到在cnblogs下(http://www.cnblogs.com/JayK/p/3429795.html)有篇文章是介绍如何添加nginx到w ...
- devexpress 给GridView添加行号
先找到 此时间gridView1_CustomDrawRowIndicator private void gridView1_CustomDrawRowIndicator(object sender, ...
- spring 5.x 系列第9篇 —— 整合mongodb (xml配置方式)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 配置文件位于resources下,项目以单 ...
- jQuery中ajax-$.getJSON,$.getScript
jQuery提供了两个特定异步加载的方法$.getJSON()方法和$.getScript()方法 $.getJSON()来加载特定的json文件 $.getScript()来加载特定的javascr ...