Model.cs
A campaign can have multiple images, that's why IEnumerable<int> ImageIdList
.
public class Campaign
{
public int Id { get; set; }
public int CreatedBy { get; set; }
public int UpdatedBy { get; set; }
public IEnumerable<int> ImageIdList { get; set; }
}
View.cshtml
I want to download all the images related to a campaign, based on the ImageIdList
, that's why I need to post all these ImageIds
when a particular Campaign is checked and download button is clicked.
@model Campaign
@{
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>
@foreach(var i in assets)
{
<div class="s_checkcol">
<input type="checkbox" name="ids" />
@foreach (var imageId in i.Where(c => c.AssetId == doc.FileDataId).SelectMany(c => c.ImageIdList))
{
<input type="hidden" name="ids" value=@(imageId)>
}
</div>
}
}
Controller.cs
public ActionResult DownloadFiles(IEnumerable<int> ids)
{
// code
}
NOTE: Only a part of code(where I'm facing the problem) is provided here. Its a DB first approach and in no way I can alter that (ORDERS).
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?