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.
asp.net
add a comment |
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.
asp.net
1
please post your code
– Gonzalo Lorieto
Nov 23 '18 at 19:42
add a comment |
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.
asp.net
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.
asp.net
asp.net
asked Nov 23 '18 at 19:40
Venu DamodaranVenu Damodaran
1
1
1
please post your code
– Gonzalo Lorieto
Nov 23 '18 at 19:42
add a comment |
1
please post your code
– Gonzalo Lorieto
Nov 23 '18 at 19:42
1
1
please post your code
– Gonzalo Lorieto
Nov 23 '18 at 19:42
please post your code
– Gonzalo Lorieto
Nov 23 '18 at 19:42
add a comment |
1 Answer
1
active
oldest
votes
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
This code requested by Gonzalo
– Venu Damodaran
Nov 24 '18 at 16:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452180%2fhow-to-insert-multiple-rows-database-in-asp-net-core-2-1%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
This code requested by Gonzalo
– Venu Damodaran
Nov 24 '18 at 16:17
add a comment |
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
This code requested by Gonzalo
– Venu Damodaran
Nov 24 '18 at 16:17
add a comment |
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
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
answered Nov 24 '18 at 15:17
Venu DamodaranVenu Damodaran
1
1
This code requested by Gonzalo
– Venu Damodaran
Nov 24 '18 at 16:17
add a comment |
This code requested by Gonzalo
– Venu Damodaran
Nov 24 '18 at 16:17
This code requested by Gonzalo
– Venu Damodaran
Nov 24 '18 at 16:17
This code requested by Gonzalo
– Venu Damodaran
Nov 24 '18 at 16:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452180%2fhow-to-insert-multiple-rows-database-in-asp-net-core-2-1%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
please post your code
– Gonzalo Lorieto
Nov 23 '18 at 19:42