Reputation: 10364
I have successfully implemended a new Index view to my application will will render all outstanding emails to a gridview which features a checkbox for each row.
My intention now is to send an email which will include details of each record selected to a designated recipient.
Once a record has been selected and the details mailed for that record, I will flag that row as 'Emailed' and filter all emailed rows out in my controller before passing the list to the view.
I am a little confused as to how I make my submit button capture all selected records that have been checked and post the entire page back to my controller where I can do my email related stuff.
Here's what I have so far, I don't think I have wired up the submit button correctly to the gridview:
@using GridMvc.Html
@Html.Grid(Model).Columns(columns =>
{
columns.Add()
.Encoded(false)
.Sanitized(false)
.Titled("Send Email?")
.SetWidth(30)
.RenderValueAs(o => Html.CheckBox("checked", false));
columns.Add(a => a.Id).Titled("Id").SetWidth(110);
columns.Add(a => a.AddressLineOneOld).Titled("Old Address")
.RenderValueAs(a => a.AddressLineOneOld + " " +
a.AddressLineTwoOld + " " +
a.AddressLineThreeOld + " " +
a.AddressLineFiveOld);
columns.Add(a => a.PostcodeOld).Titled("Old Postcode").Sortable(true);
columns.Add(a => a.AddressLineOneNew).Titled("New Address")
.RenderValueAs(a => a.AddressLineOneNew + " " +
a.AddressLineTwoNew + " " +
a.AddressLineThreeNew + " " +
a.AddressLineFiveNew);
columns.Add(a => a.PostcodeNew).Titled("New Postcode").Sortable(true);
columns.Add(a => a.Emailed).Sortable(true);
}).WithPaging(20)
<br />
<div style="float:right;">
@using (Html.BeginForm("Index", "EmailManager", FormMethod.Post, new { name = "frm", id = "frm" }))
{
<input id="btnSendEmailForSelected" type="submit" name="submitButton" value="Email Selected" class="btn btn-default" />
}
</div>
How do I post all selected rows back to the controller?
Upvotes: 3
Views: 6781
Reputation: 1946
it this line Html.CheckBox("checked", false)
add a unique name add the value for the checkbox to the Id or the unique key you have as below
Html.CheckBox("checked", false, new {name = "selectedAddressesIds", value = "UNIQUE_KEY_HERE"})
and in the action method try the following, you must write your action method to have FormCollection
var selectedIds = form.GetValues("selectedAddressesIds");
if (selectedIds != null)
{
foreach (var id in selectedIds)
{
// do what you want with the selected id.
}
}
Upvotes: 3