0

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?

Esmael
  • 69
  • 14
  • In addition, you checkbox does not bind to anything - it needs to be `@Html.CheckBoxFor(m => m[i].Delete)` and remove the pointless `new{ Data = Model })` in `BeginForm()` –  Jun 06 '17 at 21:59
  • I tried your way still doesnt work.. – Esmael Jun 06 '17 at 22:22
  • 1
    Of course it does. And its `@Html.HiddenFor(m => m[i].FirstName)` etc if you want to post back the other properties of your model (just posting back a `bool` property would make no sense). And do not completely change your question based on an answer. –  Jun 06 '17 at 22:26
  • Actually I tried to carry out your suggestion according to https://stackoverflow.com/questions/30094047/post-an-html-table-to-ado-net-datatable . and yes it seems to work boddy, Thanks – Esmael Jun 07 '17 at 10:29

0 Answers0