jeudi 30 juin 2016

Bind multiple values to a single checkbox and post it to controller

Model.cs

public class Test
{
  public int Id { get; set; }
  public int CreatedBy { get; set; }
  public int UpdatedBy { get; set; }
  public IEnumerable<int> ImageIdList { get; set; }
}

View.cshtml

@{
  Layout = "....";
  var assets = Model.AssetsInCampaign.ToList();
}
@using (Html.BeginForm("action-method", "controller", FormMethod.Post))
{
  <div class="btnSubmit">
    <input type="submit" value="Download Asset(s)" />
  </div>
  <div class="s_checkcol">
    <input type="submit" name="ids" />
    @foreach (var imageId in assets.Where(c => c.AssetId == doc.FileDataId).SelectMany(c => c.ImageIdList))
    {
        <input type="hidden" name="ids" value=@(imageId)>
    }
  </div>
}

Controller.cs

public ActionResult Action-Method(IEnumerable<int> ids)
{
    // code
}

NOTE: Only a part of code(where I'm facing the problem) is provided here.

I tried the above, but all of the ids are posted to the controller no matter how many checkboxes are selected.

Question: How should I bind the IEnumerable<int> ImageIdList property to a checkbox in View.cs and post the data to Controller.cs so that only the ids of selected checkboxes are posted?




Aucun commentaire:

Enregistrer un commentaire