My view:
<code>@model IEnumerable<HourlyLimit>
<html>
<head><title></title></head>
<body>
<form method="post" action="~/Admin/HourlyLimits/Set" enctype="multipart/form-data">
@foreach (var item in Model)
{
<input asp-for="@item.ID" hidden />
<input asp-for="@item.Date" hidden />
<input asp-for="@item.Hour" hidden />
<input asp-for="@item.Capacity" type="number">
}
<button type="submit"></button>
</body>
</html>
</code>
<code>@model IEnumerable<HourlyLimit>
<html>
<head><title></title></head>
<body>
<form method="post" action="~/Admin/HourlyLimits/Set" enctype="multipart/form-data">
@foreach (var item in Model)
{
<input asp-for="@item.ID" hidden />
<input asp-for="@item.Date" hidden />
<input asp-for="@item.Hour" hidden />
<input asp-for="@item.Capacity" type="number">
}
<button type="submit"></button>
</body>
</html>
</code>
@model IEnumerable<HourlyLimit>
<html>
<head><title></title></head>
<body>
<form method="post" action="~/Admin/HourlyLimits/Set" enctype="multipart/form-data">
@foreach (var item in Model)
{
<input asp-for="@item.ID" hidden />
<input asp-for="@item.Date" hidden />
<input asp-for="@item.Hour" hidden />
<input asp-for="@item.Capacity" type="number">
}
<button type="submit"></button>
</body>
</html>
In the get method of this action, I’m passing an IEnumerable<HourlyLimit>
to the view like this:
<code>IEnumerable<HourlyLimit> hourlyLimits = await _db.HourlyLimits
.OrderBy(h => h.Hour)
.ToListAsync();
return View(hourlyLimits);
</code>
<code>IEnumerable<HourlyLimit> hourlyLimits = await _db.HourlyLimits
.OrderBy(h => h.Hour)
.ToListAsync();
return View(hourlyLimits);
</code>
IEnumerable<HourlyLimit> hourlyLimits = await _db.HourlyLimits
.OrderBy(h => h.Hour)
.ToListAsync();
return View(hourlyLimits);
When the page is loaded, the <input />
tags are populated correctly from the existing records in the database; so it seems that the model binding is working fine.
The issue is, when I submit the form, the object that’s being passed to the post method of that action has a count
of 0
.
<code>[HttpPost]
public async Task<IActionResult> Set(IEnumerable<HourlyLimit> hourlyLimits) // hourlyLimits.Count is 0 here when I hit the breakpoint in the line below.
{
if (!ModelState.IsValid)
{
//...
}
try
{
//...
}
catch (Exception)
{
//...
}
}
</code>
<code>[HttpPost]
public async Task<IActionResult> Set(IEnumerable<HourlyLimit> hourlyLimits) // hourlyLimits.Count is 0 here when I hit the breakpoint in the line below.
{
if (!ModelState.IsValid)
{
//...
}
try
{
//...
}
catch (Exception)
{
//...
}
}
</code>
[HttpPost]
public async Task<IActionResult> Set(IEnumerable<HourlyLimit> hourlyLimits) // hourlyLimits.Count is 0 here when I hit the breakpoint in the line below.
{
if (!ModelState.IsValid)
{
//...
}
try
{
//...
}
catch (Exception)
{
//...
}
}
What’s wrong? How can I submit an IEnumerable<HouryLimit>
from the view?