在数据库MVC

时间:2017-10-10 12:42:53

标签: c# asp.net-mvc

目前,我可以将我的项目一行插入数据库,但是,我遇到的唯一问题是,如果我添加另一行数据,则不会将另一行保存为第一行。我很确定GET动作是正确的,我相信问题在于POST动作,但我不确定。非常感谢任何帮助。

这是我到目前为止的代码:

控制器

    [HttpGet]

    public ActionResult InsertData(int fiscalYear, int period, int locationID)
    {
       testViewModel viewModel = new testViewModel();

        using (BusinessLogic BLL = new BusinessLogic())
        {


            List<DataList> data = BLL.GetData(fiscalYear, period, locationID);
            List<List<DataDetail>> selecteddata = new List<List<DataDetail>>();

            selecteddata.Add(BLL.GetDataList(fiscalYear, period, locationID));

            viewModel.Selecteddata = selecteddata;
            viewModel.isFirstLoad = false;

        }

        return PartialView("_dataView", viewModel);

    }


    [HttpPost]
    public ActionResult InsertData(List<DataList> item)
    {

        using (BusinessLogic BLL = new BusinessLogic())
        {


            if (ModelState.IsValid)
            {
                try
                {

                    foreach(DataList item in items)
                    {                 
                    BLL.InsertDataItem(item.FiscalYear, item.Period, item.LocationID, item.Status, item.Comment);
                    BLL.SaveChanges();

                }
                }
                catch (Exception ex)
                {

                    ModelState.AddModelError("", ex);

                }
            }

            return RedirectToAction("Index");


        }

    }
}

}

查看

  @using (Html.BeginForm())
  {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <br />    
        <div class="panel-body">
            <table class="table table-bordered table-condensed" id="tab_logic">
                <thead>
                    <tr class="filters">
                        <th>Status</th>
                        <th>Comments</th>

                    </tr>
                </thead>

                <tbody>
                    <tr id="addItem">

                        <td><input type="text" id="Status" name="Status" class="form-control" /></td>
                        <td><input type="text" id="Comments" name="Comments" class="form-control" /></td>                   
                    </tr>
                </tbody>
            </table>
        </div>
        <a id="add_row" class="btn btn-default pull-left">Add Row</a><a id='delete_row' class="pull-right btn btn-default">Delete Row</a>
    </div>

    <div class="">

        <input type="submit" value="Save" class="btn btn-success btn-block" />
    </div>

}

的javascript

$(document).ready(function () {
        var i = 1;
        $("#add_row").click(function () {
            $('#addItem' + i).html("<td><input name='ActionItem" + i + "' type='text' class='form-control'  /> </td><td><input  name='Asignee" + i + "' type='text' class='form-control'></td><td><input  name='DateAssigned" + i + "' type='date' class='form-control'></td><td><input  name='DueDate" + i + "' type='date' class='form-control'></td><td><input  name='Status" + i + "' type='text' class='form-control'></td><td><input  name='Comments" + i + "' type='text' class='form-control'></td>");

            $('#tab_logic').append('<tr id="addItem' + (i + 1) + '"></tr>');
            i++;
        });
        $("#delete_row").click(function () {
            if (i > 1) {
                $("#addItem" + (i - 1)).html('');
                i--;
            }
        });

    });

- UPDATE -

“InsertDataItem”方法来自业务逻辑类。以下代码添加行

方法

    public int InsertDataItem(int fiscalYear, int period, int locationID, string status, string comments)
        {

            var dataList = GetData(fiscalYear, period, locationID);
            DataList items = new DataList()
            {

                FiscalYear = fiscalYear,
                Period = period,
                LocationID = locationID,

                Status = status,
                Comments = comments
            };

            // Inserts row
           entity.DataLists.Add(items);

            SaveChanges();

            return items.ID;
        }

1 个答案:

答案 0 :(得分:0)

在控制器中

List<DataList> data = BLL.GetData(fiscalYear, period, locationID);

未使用?

在post方法中:

public ActionResult InsertData(DataList item)

该项目包含我认为的单个元素(具有状态,注释等),而您应该有一个元素列表。名称DataList也具有误导性,因为它现在只包含1个元素。

如果您有兴趣,我有一篇关于如何将对象列表传递给控制器​​的帖子:)

http://debuxing.com/model-binding-to-a-list-of-objects/