This is an ASP.NET MVC project. I have a view that displays a collection of BookRentViewModel
items.
When I click on the button in the view, the view is posted to an action method called rent
. rent
action method has a parameter called items of type IEnumerable<BookRentViewModel>
.
Here is the code of the view:
@model IEnumerable<DaramSerl.Models.ViewModels.BookRentViewModel>
@using (Html.BeginForm("rent", "RentBook", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.bookName)
</th>
<th>
@Html.DisplayNameFor(model => model.isRented)
</th>
<th>
@Html.DisplayNameFor(model => model.startDate)
</th>
<th>
@Html.DisplayNameFor(model => model.endDate)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.EditorFor(modelItem => item.bookName)
</td>
<td>
@Html.EditorFor(modelItem => item.isRented)
</td>
<td>
@Html.EditorFor(modelItem => item.startDate)
</td>
<td>
@Html.EditorFor(modelItem => item.endDate)
</td>
</tr>
}
</table>
<input type="submit" value="Create" class="btn btn-primary" />
}
And here is the action method:
[HttpPost]
public async Task<ActionResult> rent(IEnumerable<BookRentViewModel> items)
{
if (ModelState.IsValid)
{
return RedirectToAction("/Index");
}
return View();
}
And here is the BookRentViewModel:
public class BookRentViewModel
{
[Key]
public int bookId{ get; set; }
public string bookName { get; set; }
public bool isRented { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
}
The problem is that items
parameter is always null when rent
action is triggered.
Any idea why items
is null and does not get the collection from the view?
UPDATE I used fiddler to see if the collection is posted to the server but it seems it cannot be parsed into BookRentViewModel items.
UPDATE3
Here is screenshot from fiddler with the collection sent from the view: