Saving Multiple records in asp.net MVC 5

Saving Multiple records in asp.net MVC 5
multiple records can be saved at once into the database using asp.net MVC 5. In this article i will show how to save multiple records at once to the database.
Add controller to your application

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyApp.Models;
namespace MyApp.Controllers
{
    public class DeviceController : Controller
    {
       
        [HttpGet]
        public ActionResult SaveMultiple()
        {
            int _recordsToEnter = 5;
            List<Device> lst = new List<Device>();
            for (int i = 1; i <= _recordsToEnter; i++)
            {
                lst.Add(new Device() { DeviceID = i, DeviceName = "", NumberOfDevices=0 });
            }
            return View(lst);
        }
        [HttpPost]
        public ActionResult SaveMultiple(List<Device> devices)
        {
            using (var _db = new HRDBEntities())
            {
                int _deviceID = _db.Devices.Max(x => (int?)x.DeviceID) ?? 0;                
                List<Device> _toSave = new List<Device>();
                foreach (var item in devices)
                {
                    if (!string.IsNullOrEmpty(item.DeviceName) && item.NumberOfDevices != 0)
                    {
                        _deviceID++;
                        _toSave.Add(new Device()
                        {
                            DeviceID = _deviceID,
                            DeviceName = item.DeviceName,
                            NumberOfDevices = item.NumberOfDevices,
                            SerialNo = item.SerialNo,
                            Comments = item.Comments
                        });
                    }                   
                }
                if (_toSave.Count > 0)
                {
                    _db.Devices.AddRange(_toSave);
                    _db.SaveChanges();
                }
            }
            return View();
        }
    }
}

Add view to the following action

[HttpGet]
public ActionResult SaveMultiple()
{
int _recordsToEnter = 5;
List lst = new List();
for (int i = 1; i <= _recordsToEnter; i++)
{
lst.Add(new Device() { DeviceID = i, DeviceName = "", NumberOfDevices=0 });
}
return View(lst);
}
View SaveMultiple.cshtml


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
@model List<MyApp.Models.Device>

@{
    ViewBag.Title = "SaveMultiple";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<style>
    .input-xs {
        width: 90px;
    }
</style>
<h2>Enter devices</h2>
<hr />
@using (Html.BeginForm())
{
    <div class="form-horizontal">
        <table class="table table-striped">
            <tr>
                <th>Sr No</th>
                <th>
                    Device Name
                </th>
                <th>
                    Device Serial
                </th>
                <th>
                    Number of Devices
                </th>
                <th>
                    Comments
                </th>
            </tr>

            @for (int i = 0; i < Model.Count; i++)
            {
                <tr>
                    <td>
                        @Html.Label(Model[i].DeviceID.ToString(), Model[i].DeviceID.ToString())
                    </td>
                    <td>
                        @Html.TextBox("devices[" + i + "].DeviceName", Model[i].DeviceName, htmlAttributes: new { @class = "form-control" })
                    </td>
                    <td>
                        @Html.TextBox("devices[" + i + "].SerialNo", Model[i].SerialNo, htmlAttributes: new { @class = "form-control input-xs", type = "number", min = 1 })
                    </td>
                    <td>
                        @Html.TextBox("devices[" + i + "].NumberOfDevices", Model[i].NumberOfDevices, htmlAttributes: new { @class = "form-control input-xs", type = "number", min = 1 })
                    </td>
                    <td>
                        @Html.TextBox("devices[" + i + "].Comments", Model[i].Comments, htmlAttributes: new { @class = "form-control" })
                    </td>

                </tr>
            }

        </table>
        <div class="row">
            <div class="col-md-offset-2 col-md-2">
                <button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-save"></i> Save</button>
            </div>
        </div>
    </div>
}


view screen shot




Enter some data into the fields and click save button
debug screen shot



Post a Comment