0

I have this class of Status :

public class Status
    {
        public Status(int id, string description)
        {
            Id = id;
            Description = description;
        }


        public int  Id { get; set; }
        public string Description { get; set; }
        public bool IsChecked { get; set; }

    }

This is my Model:

public class StatusModel
{
    public StatusModel()
    {
        Statuses = new List<Status>();
    }
    public List<Status> Statuses { get; set; }

}

and my View looks like this:

@model MVCTestApplication.Models.StatusModel

@using (Html.BeginForm("TestView", "Home"))
{
 <table>
 <thead>
       <tr>
           <th>
           Status
           </th>
       </tr>
 </thead>

 <tbody>
 @for (int i = 0; i < Model.Statuses.Count(); i++)
 {
 <tr>
 <td>

 @Html.CheckBoxFor(x => x.Statuses[i].IsChecked, new { @id = Model.Statuses[i].Id })

 @Html.LabelFor(x => x.Statuses[i].Description, Model.Statuses[i].Description)

 @Html.HiddenFor(x => x.Statuses[i].Description)

 @Html.HiddenFor(x => x.Statuses[i].Id)

 </td>
 </tr>
 }

</tbody>
</table>

    <input type="submit" name="submit" value="submit" />
}

and in controller I have:

public ActionResult TestView(StatusModel statuses)
{
//.......
}

I need to select all checked checkbox items, but when i wrote such code, I get null for my statuses variable in controller. And I don't know what I did wrong

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
Mykhalik
  • 244
  • 1
  • 4
  • 14
  • is this your complete TestView Action code? – Ehsan Sajjad Mar 19 '14 at 15:50
  • not, in this controller contains a single line of code `return View(statuses);` – Mykhalik Mar 19 '14 at 15:53
  • where are you adding items in the statuses list? – Ehsan Sajjad Mar 19 '14 at 15:57
  • I read it from database – Mykhalik Mar 19 '14 at 15:58
  • post that action as well – Ehsan Sajjad Mar 19 '14 at 15:58
  • `private DataTable ReadStatus(string connectionString) { SqlConnection conn = new SqlConnection(connectionString); SqlDataAdapter da = new SqlDataAdapter(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = @"SELECT * FROM Statuses"; da.SelectCommand = cmd; DataSet ds = new DataSet(); conn.Open(); da.Fill(ds); conn.Close(); return ds.Tables[0]; }` – Mykhalik Mar 19 '14 at 15:58
  • `private List DataTableToStatus(DataTable table) { for (int i = 0; i < table.Rows.Count; i++) { int id = (int)table.Rows[i].ItemArray[0]; string desc = (string)table.Rows[i].ItemArray[1]; statuses.Add(new Status(id, desc)); } return statuses; }` – Mykhalik Mar 19 '14 at 15:59
  • add it in the question by editing in well format its not easily readable – Ehsan Sajjad Mar 19 '14 at 15:59
  • With this methods I read data – Mykhalik Mar 19 '14 at 16:00
  • See this http://stackoverflow.com/questions/21841428/how-to-bind-checkbox-values-to-a-list-of-ints/21842127#21842127 for an example of how to bindto checkboxes – Fran Mar 19 '14 at 16:05

1 Answers1

3

you have to do something like this, read from db and pass to view:

public ActionResult TestView()
{
DataTable datatable = ReadStatus("yourconnectionstring");
List<Status> statuses = DataTableToStatus(datatable);
StatusModel model = new StatusModel();

model.Statuses = statuses 
return View(Statuses);
}


private DataTable ReadStatus(string connectionString) 
{ 
SqlConnection conn = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = conn.CreateCommand(); 
cmd.CommandText = @"SELECT * FROM Statuses"; 
da.SelectCommand = cmd; 
DataSet ds = new DataSet(); 
conn.Open(); 
da.Fill(ds); 
conn.Close(); 
return ds.Tables[0]; 
} 

private List<Status> DataTableToStatus(DataTable table) 
{ 
for (int i = 0; i < table.Rows.Count; i++) 
{ 
int id = (int)table.Rows[i].ItemArray[0];
string desc = (string)table.Rows[i].ItemArray[1];
statuses.Add(new Status(id, desc)); 
} 

return statuses; 
}

and here is your action on which data model will be posted:

[HttpPost]
public ActionResult TestView(ModelStatus model)
{
// your business logic here
}

In view set httpmethod to post:

@using (Html.BeginForm("TestView", "Home",HttpMethod="POST"))
{
 <table>
 <thead>
       <tr>
           <th>
           Status
           </th>
       </tr>
 </thead>

 <tbody>
 @for (int i = 0; i < Model.Statuses.Count(); i++)
 {
 <tr>
 <td>

 @Html.CheckBoxFor(x => x.Statuses[i].IsChecked, new { @id = Model.Statuses[i].Id })

 @Html.LabelFor(x => x.Statuses[i].Description, Model.Statuses[i].Description)

 @Html.HiddenFor(x => x.Statuses[i].Description)

 @Html.HiddenFor(x => x.Statuses[i].Id)

 </td>
 </tr>
 }
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160