I have the following ViewModel Class:
public class Data
{
public string FirstName{ set; get; }
public string LastName { set; get; }
public string Location{ set; get; }
public bool Remove{ set; get; }
}
In Controller I assign the viewmodel as following:
[HttpGet]
public async Task<ActionResult> Index()
{
List<Data> data=new List<Data>;
for(int i=0;i<1000;i++)
{
data.Add ( new Data
{
FirstName="Name"+i.ToString(),
LastName="Surname"+i.ToString(),
Location="Geolocation:"+i.ToString(),
Remove=false;
}
return View(data.ToList());
}
In my View I show all data in a table in which I can check items I would like to remove and I would like to pass alla data back to my Controller to handle the data. Here is my View:
@model List<MyProject.Models.Data>
<table>
<thead>
<tr>
<th scope="col">Firstname</th>
<th scope="col">Lastname</th>
<th scope="col">Location</th>
<th scope="col">Remove</th>
</tr>
</thead>
<tbody>
@using ( Html.BeginForm ( "Index","Home",FormMethod.Post))
{
for(int i=0;i<Model.Count;i++)
{
<tr>
<td>
@Html.CheckBoxFor(m => m[i].Remove)
</td>
<td>@Model[i].Firstname</td>
<td>@Model[i].Lastname</td>
<td>@Model[i].Location</td>
</tr>}
<button type="submit">Send</button>
}
</tbody>
</table>
An finally here is my Post action in the Controller:
[HttpPost]
public ActionResult Index(List<Data>model)
{
var result=model;
return View();
}
But I get the empty list!!
Can somone tell me What am I do wrong?