如何在MVC中保存动态添加的行 [英] How to save dynamically added rows in MVC

查看:94
本文介绍了如何在MVC中保存动态添加的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



我想将动态添加的行保存到数据库中..我已经完成了一些代码,只保存第一行。

请帮忙



我尝试过的事情:



ie,

 foreach(模型中的var i)
{< > < < span class =code-leadattribute> tbody > < tr > < td > @ Html.TextBoxFor(a => a [j] .IRateCode,new {id =RateCode,@ class =form-control,autocomplete =off})< / td > < / tr > < / tbody > < / table > ;
}

从第二行我动态添加如下:这没有保存到db。
< script < span class =code-attribute> type = text / javascript >
$( document )。ready( function (){
$( document )。on( 单击 .classAdd function (){ // on用于动态获取点击事件创建按钮

var rowCount = $(' 数据接触人)。LEN gth + 1 ;
var contactdiv = ' < tr class =data-contact-person>' +
' < td>< ; input type =textname =i-nameclass =form-control i-name01/>< / td>' +

< td>< input type =textname =[' + rowCount + ' ]。IRateCodeclass =form-control ir-code01/>< / td>' +
' < td>< input type =textid =ItemCodename =[' + rowCount + ' ]。ItemCodeclass =form-control i-code01/>< / td>' +
' < td>< input type =textid =QtyRequestedname =[' + rowCount + ' ]。QtyRequestedclass =form-control qty-req01/>< / td> ;' +
' < td>< button type =buttonid = btnAddclass =btn btn-xs btn-primary classAdd> Add More< / button>' +
' < / TR GT&;';
$(' #maintable')。append(contactdiv); // 将这些控件添加到主表类
});
< / script >

控制器:
< pre lang = cs > [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(List < mrdtl > ci)
{
foreach(var i in ci)
{
dc.MRDtl.Add(i);
}

dc.SaveChanges();
}

解决方案

document )。ready(< span class =code-keyword> function (){


document )。on( 点击 。classAdd function (){ // on用于获取动态创建按钮的click事件

var rowCount =

' .data-contact-person')。length + < span class =code-digit> 1 ;
var contactdiv = ' < tr class =data-contact-person >' +
' < td>< input type =textname =i-nameclass =form-control i-name01/>< / td>' +

' < td>< input type =textname =[' + rowCount + ' ]。IRateCodeclass =form-control ir-code01/>< / td>' +
' < td>< input type =textid =ItemCodename =[' + rowCount + ' ]。ItemCodeclass =form-control i-code01/>< / td>' +
' < td>< input type =textid =QtyRequestedname =[ ' + rowCount + ' ]。QtyRequestedclass =form-control qty-req01/>< / td>' +
' < td>< button type =buttonid =btnAddclass =btn btn-xs btn -primary classAdd> Add More< / button>' +
' < / TR>';

Dear Friends,

I want to save dynamically added rows into db.. i have done some code its save only first row.
pls, anyone help

What I have tried:

i.e,

 foreach (var i in Model)
                       {  <table><tbody><tr><td>@Html.TextBoxFor(a =>a[j].IRateCode, new { id = "RateCode", @class = "form-control", autocomplete = "off" })</td></tr></tbody></table>
}

from 2nd row i dynamically add like this: this didn't save into db.
  <script type="text/javascript">
            $(document).ready(function () {
                $(document).on("click", ".classAdd", function () { //on is used for getting click event for dynamically created buttons

                    var rowCount = $('.data-contact-person').length + 1;
                    var contactdiv = '<tr class="data-contact-person">' +
                '<td><input type="text" name="i-name" class="form-control i-name01" /></td>' +

                                    '<td><input type="text" name="['+rowCount+'].IRateCode" class="form-control ir-code01" /></td>' +
                                    '<td><input type="text" id="ItemCode" name="[' + rowCount + '].ItemCode" class="form-control i-code01" /></td>' +
                                    '<td><input type="text" id="QtyRequested" name="[' + rowCount + '].QtyRequested" class="form-control qty-req01" /></td>' +
                '<td><button type="button" id="btnAdd" class="btn btn-xs btn-primary classAdd">Add More</button>' +                
                '</tr>';
                    $('#maintable').append(contactdiv); // Adding these controls to Main table class
                });
</script>
 
Controller:
<pre lang="cs">[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(List<mrdtl> ci)
        {
foreach (var i in ci)
                    {
                        dc.MRDtl.Add(i);
                    }

                    dc.SaveChanges();
}

解决方案

(document).ready(function () {


(document).on("click", ".classAdd", function () { //on is used for getting click event for dynamically created buttons var rowCount =


('.data-contact-person').length + 1; var contactdiv = '<tr class="data-contact-person">' + '<td><input type="text" name="i-name" class="form-control i-name01" /></td>' + '<td><input type="text" name="['+rowCount+'].IRateCode" class="form-control ir-code01" /></td>' + '<td><input type="text" id="ItemCode" name="[' + rowCount + '].ItemCode" class="form-control i-code01" /></td>' + '<td><input type="text" id="QtyRequested" name="[' + rowCount + '].QtyRequested" class="form-control qty-req01" /></td>' + '<td><button type="button" id="btnAdd" class="btn btn-xs btn-primary classAdd">Add More</button>' + '</tr>';


这篇关于如何在MVC中保存动态添加的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆