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

10
  • is this your complete TestView Action code? Commented Mar 19, 2014 at 15:50
  • not, in this controller contains a single line of code return View(statuses); Commented Mar 19, 2014 at 15:53
  • where are you adding items in the statuses list? Commented Mar 19, 2014 at 15:57
  • I read it from database Commented Mar 19, 2014 at 15:58
  • post that action as well Commented Mar 19, 2014 at 15:58

1 Answer 1

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>
 }
Sign up to request clarification or add additional context in comments.

5 Comments

It works but I have other problem. When I check some checkboxes on page and press button I get list of all check buttons
yes it will post the checkboxes values which are checked not the ones that are not checked
i think it will post null values against the ones not checked
for example I have 5 check boxes and when I select 2. I want to post those elements, but currently I post all of this
whole model will be posted on action, you have to filter them in action before doing any db operation according to your business logic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.