类别中包含一个产品的集合属性,如何向数据库添加一条类别记录的同时,添加任意多个产品。

    public class Product
    {
        [DisplayName("产品名称")]
        public string Name { get; set; }
    }
 
    public class Category
    {
        [DisplayName("类别名称")]
        public string Name { get; set; }
        private IList<Product> _products = new List<Product>();
 
        public IList<Product> Products
        {
            get { return _products; }
            set { _products = value; }
        }
    }  

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; } 
   
□ 思路

控制器方法能接收的格式为:

Category.Name

Category.Products[0].Name

Category.Products[1].Name

...

前台视图使用jquery动态生成input,并把input的name属性设置成Category.Products[i].Name格式。

 

□ Home/Index.cshtml视图

@model AddingMultipleNestedData.Models.Category
 
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
 
@using (Html.BeginForm("Create", "Home", FormMethod.Post, new {id = "addForm"}))
{
    <div>
        @Html.LabelFor(m => m.Name)
        @*@Html.EditorFor(m => m.Name)*@
        @Html.TextBox("Category.Name")
    </div>
    <div id="products"></div>
    <div>
        <input id="btnAddProduct" type="button" value="添加产品"/>
    </div>
    <div>
        <input type="submit" value="提交"/>
    </div>
}
 
@section scripts
{
    <script type="text/javascript">
        $(function() {
            var noOfProducts = 0;
            $('#btnAddProduct').click(function() {
                var product = getNestedName("Category.Products", noOfProducts);
                noOfProducts++;
                $('#products').append("<input type='text' name='"+product+".Name' /><p>");
            });
 
        });
 
        function getNestedName(itemName, itemNumber) {
            return (itemName + "[" + itemNumber + "]");
        }
    </script>
}
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

没有添加产品前:

 

添加产品集合:

□ HomeController

        public ActionResult Index()
        {
            return View();
        }
 
        [HttpPost]
        public ActionResult Create(Category category)
        {
            return View();
        }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

断点调试:       

 

  更新 2014.05.07

以上做法至少有二个弊端:

1、无法对新添加的集合属性对应的个体进行验证。

2、如果破坏集合元素的连续性,会影响控制器不能完全接收所有集合记录。

Category.Name

Category.Products[0].Name

Category.Products[3].Name  
Category.Products[6].Name  

控制器只能接收到集合中的第一条记录,即Category.Products[0].Name,也就是说,一旦集合元素不是连续的,控制器将不能接收到全部集合记录。

把noOfProducts++;改成noOfProducts = noOfProducts + 3;

 

断点调试: 

只能接收到一条记录。

在下一篇中,将寻找一种既能验证集合元素,控制器又能接收不连续集合元素的解决方案!

 

  关于3楼刘慧心所提的问题

总体来说,问题处在:

<input type="hidden" name="CgdModel.Ubis.Index" value="indexA" />

<input type="text" name="CgdModel.Ubis[indexA].Fdt"...

<input type="text" name="CgdModel.Ubis[indexA].Memo...

 

每一组Ubis属性所对应的Model,都对应着value值不同的隐藏域,大体应该这样:

<input type="hidden" name="CgdModel.Ubis.Index" value="0" />

<input type="text" name="CgdModel.Ubis[0].Fdt"...

<input type="text" name="CgdModel.Ubis[0].Memo...

 

<input type="hidden" name="CgdModel.Ubis.Index" value="1" />

<input type="text" name="CgdModel.Ubis[1].Fdt"...

<input type="text" name="CgdModel.Ubis[1].Memo...

...

□ 假设您的Models是这样:

    public class CgdModel
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public IList<Ubis> Ubis { get; set; }
    }
 
    public class Ubis
    {
        public string Fdt { get; set; }
        public string Memo { get; set; }
    }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

□ 假设CgdController是这样:

        public ActionResult Create()
        {
            return View(new CgdModel());
        }
 
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CgdModel cgdModel)
        {
            if (!ModelState.IsValid)
            {
                return View(cgdModel);
            }
            return Content("ok");
        }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

□ CgdController/Create.cshtml大致是这样:

@model VariableCollection.Models.CgdModel
 
@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
 
@using (Html.BeginForm())
{
     @Html.AntiForgeryToken()
    <input type="submit" value="产生采购单" />
    <fieldset>
        <legend>采购单</legend>
        
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
    </fieldset>
    
    <fieldset>
        <legend>明细</legend>
        <table>
        @for (int i = 0; i < 3; i++)
        {
            <tr>
                <td><input type="hidden" name="Ubis.Index" value="@i" /></td>
                <td>Fdt @i<input type="text" name="Ubis[@i].Fdt" /></td>
                <td>Memo @i<input type="text" name="Ubis[@i].Memo" /></td>
            </tr> 
        }
        </table>
    </fieldset>
}
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

 

断点调试,可以接收到来自视图的集合所有元素数据:

MVC批量添加,增加一条记录的同时添加N条集合属性所对应的个体的更多相关文章

  1. 当前时间、前n天、后n天、取前n条记录、从第n条开始取m条

    当前时间:NOW() 前n天:DATE_SUB(NOW(),INTERVAL n DAY) 后n天:DATE_SUB(NOW(),INTERVAL -n DAY) 取前n条记录:SELECT * FR ...

  2. SQL查找TCar表中同一辆车前后两条记录的CarId,两条记录中有多个字段值一样

    查询同一个表中某一字段值相同的记录 select * from 表名 where 字段 in(select 字段 from 表名 group by 字段 having count(1)>1) s ...

  3. 【MyBatis】【SQL】没有最快,只有更快,从一千万条记录中删除八百万条仅用1分9秒

    这次直接使用delete from emp where cdate<'2018-02-02',看看究竟会发生什么. Mapper里写好SQL: <?xml version="1. ...

  4. 【MyBatis】【SQL】删除最快纪录诞生,从一千万条记录中删除八百万条仅用2分6秒

    在 https://www.cnblogs.com/xiandedanteng/p/11669629.html 里我做个一个循环按时间查ID并删除之的程序,运行时间是4分7秒. 但是这个程序走了很多次 ...

  5. mysql将一张表中多条记录按联系整合成一条

    现有表如下:id time is_login 3 2012-07-03 11:20:20 13 2012-07-03 11:25:20 04 2012-07-03 12:30:20 14 2012-0 ...

  6. MVC批量更新,可验证并解决集合元素不连续控制器接收不完全的问题

    在"MVC批量添加,增加一条记录的同时添加N条集合属性所对应的个体"中,有2个问题待解决: 1.由jquery动态生成了表单元素,但不能实施验证. 2.一旦集合元素不连续,控制器就 ...

  7. ThinkPHP与EasyUI整合之二(datagrid):删除多条记录

    学习EasyUI已有一段时间了,现在开始逐步把平时学习的细节和难点记录下来. 1. datagrid选中多条记录的语句是: var rows = $('#dg').datagrid('getSelec ...

  8. MSSQL—按照某一列分组后取前N条记录

    以前在开发的时候遇到过一个需求,就是要按照某一列进行分组后取前几条数据,今天又有同事碰到了,帮解决了之后顺便写一篇博客记录一下. 首先先建一个基础数据表,代码如下: IF OBJECT_ID(N'Te ...

  9. php实现只保留mysql中最新1000条记录

    这篇文章主要介绍了php实现只保留mysql中最新1000条记录的方法和相关示例及数据库结构,十分的全面,有需要的小伙伴可以参考下. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...

随机推荐

  1. Codeforces 798C - Mike and gcd problem(贪心+数论)

    题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1, ...

  2. 关于更新SQLserver统计信息的存储过程

    1.建立t_mon_table_stat 用于存过需要更新统计信息的表 2.查找需要更新统计信息的表 insert into t_mon_table_stat SELECT DISTINCT SP.r ...

  3. 能ping但是无法上网

    管理员权限,执行以下这个命令就好了. netsh winsock reset 配置问题,重置windows socket.

  4. Atom编辑器试用(Win7)

     看到Atom的官网(https://atom.io/)上说,Atom是A hackable text editor for the 21st Century,是一个基于V8引擎的editor,于是决 ...

  5. Kafka(五)Kafka的API操作和拦截器

    一 kafka的API操作 1.1 环境准备 1)在eclipse中创建一个java工程 2)在工程的根目录创建一个lib文件夹 3)解压kafka安装包,将安装包libs目录下的jar包拷贝到工程的 ...

  6. box-sizing 盒子模型不改变大小

    参考 box-sizing 相关说明 在需要设置元素的内边距或者外边距而不想改变整体大小时,可以使用该属性,记录之. .simple { width: 500px; margin: 20px auto ...

  7. ueditor初始化

    一.下载文件复制到项目中 二.复制表情文件 三.复制列表图片 四.修改ueditor.config.js文件 五.接着修改net文件下config.json文件 六.完蛋了,不支持IE8,版本替换为了 ...

  8. gluster学习(二)

    GlusterFS卷管理 一.卷基本选项设置 1. 调整高速缓存大小 gluster volume set test-volume performance.cache-size 256MB 2. 详细 ...

  9. poj3268 Silver Cow Party(两次SPFA || 两次Dijkstra)

    题目链接 http://poj.org/problem?id=3268 题意 有向图中有n个结点,编号1~n,输入终点编号x,求其他结点到x结点来回最短路长度的最大值. 思路 最短路问题,有1000个 ...

  10. PHP程序员未来路在何方

    PHP 从诞生到现在已经有20多年历史,从Web时代兴起到移动互联网退潮,互联网领域各种编程语言和技术层出不穷, Node.js . GO . Python 不断地在挑战 PHP 的地位.这些技术的推 ...