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

    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. FZU 1901 Period II(KMP循环节+公共前后缀)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1901 题目大意:题目大意求出所有p满足s[i]=s[i+p](i<=len-p) 解题思路: 其实就是要 ...

  2. 2018JAVA复习摘要

    由于公司内部原因,2018年感觉自己可能会换个新环境:虽然时间尚未确定,但还是得提前做好防范,毕竟面试复习是需要时间好好准备才能拿到自己理想的offer.打算从清明节之后开始好复习基本知识要点,先整理 ...

  3. AARRR:数据运营模型

    一.基础知识 产品经理  <增长黑客>数据分析基础 -- 获取(Acquisition)-- 激活(Activation)-- 留存(Retention)-- 收入(Revenue)-- ...

  4. 浏览器被hao123,hao524劫持的解决办法

    今天研究(翻,墙),装了几个插件,什么云帆.外遇.蓝灯 后来我的google浏览器被hao123劫持,百度浏览器被hao524劫持 删除浏览器快捷方式.属性目标里的后缀,过不多久又被劫持,把我搞毛了 ...

  5. CVE-2009-3459

     Adobe Acrobat和Reader都是美国Adobe公司开发的非常流行的PDF文件阅读器.         Adobe Reader和Acrobat 7.1.4之前的7.x版本,8.1.7之前 ...

  6. 搜索入门之dfs--经典的迷宫问题解析

    今天来谈一下dfs的入门,以前看到的dfs入门,那真的是入门吗,都是把dfs的实现步骤往那一贴,看完是知道dfs的步骤了,但是对于代码实现还是没有概念.今天准备写点自己的心得,真的是字面意思--入门. ...

  7. CCF CSP 201312-3 最大的矩形

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201312-3 最大的矩形 问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i( ...

  8. USACO 5.3 Milk Measuring

    Milk MeasuringHal Burch Farmer John must measure Q (1 <= Q <= 20,000) quarts of his finest mil ...

  9. jquery实现checkbox的单选和全选

    一.思路 全选:判断“全选”checkbox的状态,如果选中则把tbody下所有的checkbox选中,反之 单选:主要是判断有没有全选,如果不是选中状态就把全选的checkbox状态设置为false ...

  10. python日常总结

    1. post请求中是否可以在url中携带请求体信息? 可以.Get请求时,请求体放在URL中; POST请求,请求体既可以是Form表单中的数据 也可以在请求的URL地址中放请求体信息. 如: &l ...