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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…