0

i would like to get the list of checkboxes to the controller.

 foreach (var item in Model.Billeder)
  {
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(ModelItem => item.Overskrift, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.DisplayFor(model => item.Overskrift)
        <div class="col-md-10">
            <div class="checkbox">
                @Html.EditorFor(ModelItem => item.Emailbool)
                @Html.ValidationMessageFor(ModelItem => item.Emailbool, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>


}


    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Send" class="btn btn-default" />
        </div>
    </div>

in tilbud i have a

public virtual ICollection<Billed> Billeder { get; set; }

i would like for it to come into the controller so i can sort them

public ActionResult MailTilbud(Tilbud tb)

1 Answers1

0

The for-each loop wouldn't work because the checkbox element name wouldn't be properly created with index which would cause it to be not posted back in controller, so change it to use for loop :

for (int i=0 i <Model.Billeder.Count; i++)
{
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(ModelItem => Model.Billeder[i].Overskrift, htmlAttributes: new { @class = "control-label col-md-2" }) 
        @Html.DisplayFor(model => Model.Billeder[i].Overskrift)
        <div class="col-md-10">
            <div class="checkbox">
                @Html.EditorFor(ModelItem => Model.Billeder[i].Emailbool)
                @Html.ValidationMessageFor(ModelItem => Model.Billeder[i].Emailbool, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
}

and you would need to modify your model to have IList<T>, as ICollection<T> does not have indexing support.

public virtual IList<Billed> Billeder { get; set; }

and if the above property is in your model which is mapped to a table using Entity Framework, then you would need to create ViewModel class specific to view instead of modifying the DTO model.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160