Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

how to insert multiple rows database in asp.net core 2.1

I have one table which contains static data with 5 columns and 4 rows. I have another table which would accept input based on static table. I am trying to build a view with table where all field from static Db table is displayed and few text box so that user could fill in the data. But when I click on submit button no data is saved in second table. Can anyone help me with this. How could I submit multiple rows to database based on static database table.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Following are the codes:

View Model:

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

View

@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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...