2

I'm trying to pass the values of checkboxs from view to controller. here are my 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 is i can get the list of selected items then do some work with them. In parameter of the method. I had tried:

List<Models.ORDER_HEADER_INFO> model

and

IEnumerable<TIS.Models.ORDER_HEADER_INFO> model

but when i debug the model is still null.

Many thanks!

9
  • 1
    Your foreach loop is generating duplicate id attributes (invalid html) and name attributes (which can't be bound to a collection) for the checkboxes. Use a for loop of a custom EditorTemplate for typeof ORDER_HEADER_INFO - for(int i = 0; i < Model.Count; i++) { @Html.CheckBoxFor(m => m[0].checkExport) } Inspect the html before and after to see the difference Commented Apr 23, 2015 at 4:46
  • When i use for(int i = 0; i < Model.Count; i++) there is a notice that Operator "<" can not be applied to operands of type "int" and "method group" Commented Apr 23, 2015 at 4:59
  • Make the model @model IList<TIS.Models.ORDER_HEADER_INFO> (or List<...>) Commented Apr 23, 2015 at 5:02
  • How can i replace this "item.PRODUCT_START_DATE". Sorry, I'm totally new with MVC 4 Commented Apr 23, 2015 at 5:08
  • 1
    Exactly the same way - inside the for loop - @Html.DisplayFor(m => m[0].PRODUCT_START_DATE) (dreadful naming BTW - follow convention and use ProductStartDate). But you have other problems as well. Your model has a property named MODEL and you have also named the parameter in your method model so binding will fail (you need to change one or the other) Commented Apr 23, 2015 at 5:15

1 Answer 1

0

As others have mentioned, you simply need to rewrite your foreach to be a for loop so that it applies indexing to the name attributes of the HTML fields, in order for the model binder to do it's work. Change your foreach to be:

@for (int i = 0; i < 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>        
}
Sign up to request clarification or add additional context in comments.

Comments

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.