I have a Generic List that it contains 4 value .
how can I get my generic list values by index ? I want to get all values in generic list
this is my code :
var Checked = (form.GetValues("assignChkBx")).ToList();
string str = "";
for (int i = 0; i < Checked.Count; i++)
{
str = str + Checked[i]. +",";
}
in this code I got all checkboxes values that checked . Now I want to get all values . how can I get values ?
Your question about the generic list is very misleading and I suspect that it has nothing to do with your real problem.
Depending on how you generated the checkboxes inside your view that might be possible or not. If you hardcoded them using directly an
<input type="checkbox">tag values of checkboxes that were not checked will never be sent to the server – that’s how HTML checkboxes work. In this case you will not be able to get all values. If on the other hand you used theHtml.CheckBoxForhelper to generate them then you will notice that this helper adds a hidden field to each checkbox in order to send all values. This helper operates on boolean values though.So I would recommend you creating a view model that will contain 2 properties: one holding the values you are interested in and one boolean property indicating whether the user selected this value or not in the view:
and then have a view model which has collection of those items:
Now inside your view you can render those values like this:
and finally inside the controller action that this form will be submitted to you will be able to get all values: