asp.net mvc - Null error when passing checkbox value from view to controller MVC 4 -


i'm trying pass values of checkboxs view controller. here code

in model:

public partial class order_header_info {    //many other fields      public bool checkexport { get; set; } } 

in controller:

 [httppost]  public void exportcsv(list<models.order_header_info> model) {         foreach (models.order_header_info item in model) {               if (item.checkexport) {                  //do somethings               }          } 

in view:

@model ienumerable<tis.models.order_header_info> @using (html.beginform("exportcsv", "mkp_004", formmethod.post)){   <input type="submit" value="exportcsv" />   @foreach (var item in model)     {datetime deadline = new datetime(2015, 04, 12);         var classname = (item.product_start_date >= deadline) ? "selected" : null;         <tr class="@classname">             <td>                 @html.actionlink(item.order_no, "mkp_003", "mkp_003", new { id = item.order_no }, new { })             </td>             <td>                 @html.displayfor(modelitem => item.model)             </td>             <td>                 @html.displayfor(modelitem => item.pjno)             </td>             <td>                 @html.displayfor(modelitem => item.delivery_destination)             </td>             <td>                 @html.displayfor(modelitem => item.product_start_date)             </td>             <td>                 @html.displayfor(modelitem => item.finish_flag)             </td>             <td>                @html.checkboxfor(modelitem => item.checkexport)             </td>         </tr>             } </table> } 

my expected outcome can list of selected items work them. in parameter of method. had tried:

list<models.order_header_info> model 

and

ienumerable<tis.models.order_header_info> model 

but when debug model still null.

many thanks!

as others have mentioned, need rewrite foreach for loop applies indexing name attributes of html fields, in order model binder it's work. change foreach be:

@for (int = 0; < model.count(); i++) {datetime deadline = new datetime(2015, 04, 12);     var classname = (model[i].product_start_date >= deadline) ? "selected" : null;     <tr class="@classname">         <td>             @html.actionlink(model[i].order_no, "mkp_003", "mkp_003", new { id = model[i].order_no }, new { })         </td>         <td>             @html.displayfor(m => m[i].model)         </td>         <td>             @html.displayfor(m => m[i].pjno)         </td>         <td>             @html.displayfor(m => m[i].delivery_destination)         </td>         <td>             @html.displayfor(m => m[i].product_start_date)         </td>         <td>             @html.displayfor(m => m[i].finish_flag)         </td>         <td>            @html.checkboxfor(m => m[i].checkexport)         </td>     </tr>         } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -