Hello, I have several buttons in the toolbar of the grid. When the user activates a button, the action method must read the status of a column of checkboxes in the grid.
.ToolBar(commands => commands.Custom()
.HtmlAttributes(new { id = "Delete" })
.Text("Funz1")
.Action("Funz1Action", "AssetsList", new { TypeOp = "Op1" })
)
c.Template(t => { %>
<input value="<%= t.IdName %>" name="CheckBoxId" type="checkbox" />
<% }).Title("cb");
public ActionResult Funz1Action (string TypeOp, string[] CheckBoxId) {
return RedirectToAction("Index");
}
The content of TypeOp is correct, while CheckBoxId is null even if they were selected in the grid.
How can I read in the method Funz1Action the state of the checkboxes?
Thanks.
4 Answers, 1 is accepted
I'm also facing similar issue. I have a checkBox in my grid and I need to Export the checked Rows to an Excel (I'm having the button in the toolbar).
When I'm passing the checked row ID's to the Export method at all times it is NULL..
Thanks
Shreejit
I'm really looking forward for any solution to my problem and any help would be great.
If anyone can provide some sample code on how to export to Excel (either .XLS or .XLSX not .CSV) from a Telerik MVC grid. I need to export only the selected rows from the grid.
Thanks in advance,
Shreejit
I use the following code to export to excel:
public FileStreamResult excel(string year, string tab, string page, string orderBy, string groupBy, string filterBy) { string filter = "?" + page; if (!string.IsNullOrEmpty(orderBy)) filter += "&orderBy=" + orderBy; if (!string.IsNullOrEmpty(groupBy)) filter += "&groupBy=" + groupBy; if (!string.IsNullOrEmpty(filterBy)) filter += "&filter=" + filterBy; Session["filter"] = filter; MemoryStream ms = new MemoryStream(); StreamWriter stream = new StreamWriter(ms); tab = tab.Substring(0, 1).ToUpper() + tab.Substring(1); switch (tab) { case "Statewide": stream.WriteLine("Name, Office, Party, Contributions, Expenditures, Debt, Ind Sup, Ind Opp"); doc = XDocument.Load(ConfigurationManager.AppSettings.Get("xmlpath") + "\\statewide" + year + ".xml"); var qsw = from c in doc.Descendants("candidate") select new can_sw_model { name = c.Attribute("name").Value.Replace(",", ""), office = c.Parent.Attribute("name").Value.ToUpper(), party = c.Attribute("party").Value, raised = decimal.Parse(c.Attribute("raised").Value.Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), spent = decimal.Parse(c.Attribute("spent").Value.Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), debt = decimal.Parse(c.Attribute("debt").Value.Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), ind_supporting = decimal.Parse(c.Attribute("iefor").Value.Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), ind_opposing = decimal.Parse(c.Attribute("ieagainst").Value.Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), }; qsw = (IEnumerable<can_sw_model>)qsw.AsQueryable().ToGridModel(1, 0, orderBy, "", filterBy).Data; foreach (can_sw_model mod in qsw) { stream.WriteLine(mod.name.Replace(",", "") + ", " + mod.office.Replace(",", "") + ", " + mod.party + ", " + mod.raised.ToString() + ", " + mod.spent.ToString() + ", " + mod.debt.ToString() + ", " + mod.ind_supporting.ToString() + ", " + mod.ind_opposing.ToString()); } break; case "Legislative": stream.WriteLine("Name, District, Office, Position, Party, Contributions, Expenditures, Debt, Ind Sup, Ind Opp"); doc = XDocument.Load(ConfigurationManager.AppSettings.Get("xmlpath") + "\\legislative" + year + ".xml"); var qleg = from c in doc.Descendants("candidate") select new can_leg_model { name = c.Attribute("name").Value, district = c.Parent.Parent.Attribute("number").Value, office = c.Parent.Attribute("name").Value.ToUpper(), position = getPosition(c), party = c.Attribute("party").Value, raised = decimal.Parse(c.Attribute("raised").Value.Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), spent = decimal.Parse(c.Attribute("spent").Value.Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), debt = decimal.Parse(c.Attribute("debt").Value.Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), ind_supporting = decimal.Parse(c.Attribute("iefor").Value.Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), ind_opposing = decimal.Parse(c.Attribute("ieagainst").Value.Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), }; qleg = (IEnumerable<can_leg_model>)qleg.AsQueryable().ToGridModel(1, 0, orderBy, "", filterBy).Data; foreach (can_leg_model mod in qleg) { stream.WriteLine(mod.name.Replace(",", "") + ", " + mod.district + ", " + mod.office.Replace(",", "") + ", " + mod.position + ", " + mod.party + ", " + mod.raised.ToString() + ", " + mod.spent.ToString() + ", " + mod.debt.ToString() + ", " + mod.ind_supporting.ToString() + ", " + mod.ind_opposing.ToString()); } break; case "Judicial": stream.WriteLine("Name, Court, Division, Position, Contributions, Expenditures, Debt, Ind Sup, Ind Opp"); doc = XDocument.Load(ConfigurationManager.AppSettings.Get("xmlpath") + "\\judicial" + year + ".xml"); var qjud = from c in doc.Descendants("candidate") select new can_jud_model { name = c.Attribute("name").Value, court = getCourt(c), division = getDivision(c), position = getCourtPosition(c), raised = decimal.Parse(c.Attribute("raised").Value.Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), spent = decimal.Parse(c.Attribute("spent").Value.Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), debt = decimal.Parse(c.Attribute("debt").Value.Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), ind_supporting = decimal.Parse(c.Attribute("iefor").Value.Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), ind_opposing = decimal.Parse(c.Attribute("ieagainst").Value.Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), }; qjud = (IEnumerable<can_jud_model>)qjud.AsQueryable().ToGridModel(1, 0, orderBy, "", filterBy).Data; foreach (can_jud_model mod in qjud) { stream.WriteLine(mod.name.Replace(",", "") + ", " + mod.court + ", " + mod.division + ", " + mod.position + ", " + mod.raised.ToString() + ", " + mod.spent.ToString() + ", " + mod.debt.ToString() + ", " + mod.ind_supporting.ToString() + ", " + mod.ind_opposing.ToString()); } break; case "Local": stream.WriteLine("Name, Locality, Office, Position, Party, Contributions, Expenditures, Debt, Ind Sup, Ind Opp"); doc = XDocument.Load(ConfigurationManager.AppSettings.Get("xmlpath") + "\\local" + year + ".xml"); var qloc = from c in doc.Descendants("candidate") select new can_loc_model { name = c.Attribute("name").Value, locality = c.Parent.Parent.Parent.Attribute("name").Value.ToUpper(), office = c.Parent.Parent.Attribute("name").Value.ToUpper(), position = getLocalPosition(c), party = c.Attribute("party").Value, raised = decimal.Parse(((string)c.Attribute("raised")).Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), spent = decimal.Parse(((string)c.Attribute("spent")).Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), debt = decimal.Parse(((string)c.Attribute("debt")).Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), ind_supporting = decimal.Parse(((string)c.Attribute("iefor")).Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), ind_opposing = decimal.Parse(((string)c.Attribute("ieagainst")).Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), }; qloc = (IEnumerable<can_loc_model>)qloc.AsQueryable().ToGridModel(1, 0, orderBy, "", filterBy).Data; foreach (can_loc_model mod in qloc) { stream.WriteLine(mod.name.Replace(",", "") + ", " + mod.locality.Replace(",", "") + ", " + mod.office + ", " + mod.position + ", " + mod.party + ", " + mod.raised.ToString() + ", " + mod.spent.ToString() + ", " + mod.debt.ToString() + ", " + mod.ind_supporting.ToString() + ", " + mod.ind_opposing.ToString()); } break; case "Surplus": stream.WriteLine("Name, Transfers, Expenditures"); doc = XDocument.Load(ConfigurationManager.AppSettings.Get("xmlpath") + "\\surplus.xml"); var qsurp = from c in doc.Descendants("surplus") select new can_surplus_model { name = c.Attribute("name").Value, transfers = decimal.Parse(((string)c.Attribute("transfers")).Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), expenditures = decimal.Parse(((string)c.Attribute("spent")).Replace("$", "").Replace(",", "").Replace("(", "-").Replace(")", "")), }; qsurp = (IEnumerable<can_surplus_model>)qsurp.AsQueryable().ToGridModel(1, 0, orderBy, "", filterBy).Data; foreach (can_surplus_model mod in qsurp) { stream.WriteLine(mod.name.Replace(",", "") + ", " + mod.transfers.ToString() + ", " + mod.expenditures.ToString()); } break; } stream.Flush(); ms.Position = 0; return File(ms, "text/csv", tab + "Candidates-" + year + ".csv"); } I hope it helps!
Bob