【问题标题】:how to insert multiple rows database in asp.net core 2.1如何在 asp.net core 2.1 中插入多行数据库
【发布时间】:2018-11-23 19:40:22
【问题描述】:

我有一张表,其中包含 5 列和 4 行的静态数据。我有另一个表可以接受基于静态表的输入。我正在尝试使用表构建一个视图,其中显示静态 Db 表中的所有字段和几个文本框,以便用户可以填写数据。但是当我点击提交按钮时,没有数据保存在第二个表中。谁能帮我这个。如何根据静态数据库表向数据库提交多行。

【问题讨论】:

  • 请发布您的代码

标签: asp.net


【解决方案1】:

以下是代码:

查看模型:

public class DBM2BillingViewModel
    {
        public List<DatabaseM2> databaseM2List { get; set; }

        [Display(Name = "Items")]
        public string Name { get; set; }

        [Display(Name = "PO Item No.")]
        public int POItemNo { get; set; }

        [Display(Name = "Date of Effect")]
        public DateTime DateOfEffect { get; set; }

        [Display(Name = "Baseline Volume")]
        public float baselineVolume { get; set; }

        [Display(Name = "Billing Month")]
        public string BillingMonth { get; set; }

        [Display(Name = "Monthly Device Count")]
        public float DeviceCount { get; set; }
    }

**Controller**
//Get Method
        public IActionResult Create()
        {
            DBM2BillingViewModel dbm2billingVM = new DBM2BillingViewModel();
            dbm2billingVM.databaseM2List = _context.databaseM2s.ToList();
            return View(dbm2billingVM);
        }

        //Post Method
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(DBM2BillingViewModel model)
        {
            foreach(var dbm2 in model.databaseM2List)
            {
                DeviceBilling addModel = new DeviceBilling();
                addModel.Name = dbm2.Name;
                addModel.TrackName = "Database M2";
                addModel.POItemNo = dbm2.POItemNo;
                addModel.DateOfEffect = dbm2.DateOfEffect;
                addModel.baselineVolume = dbm2.BaselineVolume;
                addModel.BillingMonth = model.BillingMonth;
                addModel.DeviceCount = model.DeviceCount;
                _context.deviceBillings.Add(addModel);
            }
            _context.SaveChanges();
            return RedirectToAction(nameof(Index));
        }
    enter code here

查看

@model FinancantPro.Models.DeviceCountModel.DBM2BillingViewModel
@{
    ViewData["Title"] = "Create";
}

<div class="container">
    <table class="table table-striped border">
        <thead>
            <tr class="table-info">
                <th>@Html.DisplayName("Item Name")</th>
                <th>@Html.DisplayName("PO Item No#")</th>
                <th>@Html.DisplayName("Date Of Effect")</th>
                <th>@Html.DisplayName("Baseline Volume")</th>
                <th>@Html.DisplayName("Billing Month")</th>
                <th>@Html.DisplayName("Device Count")</th>
            </tr>
        </thead>

        <tbody>
            @for (int i = 0; i < Model.databaseM2List.Count; i++)
            {
                <tr>
                    <td>
                        @Html.HiddenFor(d => d.databaseM2List[i].Id)
                        @Html.DisplayFor(d => d.databaseM2List[i].Name)
                    </td>
                    <td>
                        @Html.DisplayFor(d => d.databaseM2List[i].POItemNo)
                    </td>
                    <td>
                        @Html.DisplayFor(d => d.databaseM2List[i].DateOfEffect)
                    </td>
                    <td>
                        @Html.DisplayFor(d => d.databaseM2List[i].BaselineVolume)
                    </td>
                    <td>
                        @Html.TextBoxFor(d => d.BillingMonth, new { @class = "form-control" })
                    </td>
                    <td>
                        @Html.TextBoxFor(d => d.DeviceCount, new { @class = "form-control" })
                    </td>
                </tr>
            }
        </tbody>
    </table>
    <div class="form-group">
        <input type="submit" class="btn btn-primary" value="Create" />

    </div>
</div>

**************************
If I add list in View I am not able to resolve the Model

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-07
    • 2016-04-25
    • 2019-04-12
    • 1970-01-01
    • 2010-10-10
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多