Html.Telerik().Grid(Model)
.Name(
"grid1")
.DataBinding(d => d.Ajax().Select(
"ajax_sw_candidates", "Candidate"))
.Columns(c =>
{
c.Bound(o => o.name);
c.Bound(o => o.office);
c.Bound(o => o.party);
c.Bound(o => o.raised).HtmlAttributes(
new { @style = "text-align:right" }).Format("{0:C}");
c.Bound(o => o.spent).HtmlAttributes(
new { @style = "text-align:right" }).Format("{0:C}");
c.Bound(o => o.ind_supporting).HtmlAttributes(
new { @style = "text-align:right" }).Format("{0:C}");
c.Bound(o => o.ind_opposing).HtmlAttributes(
new { @style = "text-align:right" }).Format("{0:C}");
})
.PrefixUrlParameters(
false)
.Pageable(p => p.PageSize(15)
.Style(
GridPagerStyles.NextPreviousAndNumeric)
.Position(
GridPagerPosition.Bottom))
.Sortable()
.Filterable()
.Groupable()
.Render();
The CandidateController:
public ActionResult sw_candidates()
{
// read XML file to get data
doc =
XDocument.Load(@"C:\Workspace\Visual Studio 2008\Solutions\MvcQuerySystem\MvcQuerySystem\Content\xml\statewide2008.xml");
var q = from c in doc.Descendants("candidate")
select new can_sw_model
{
name = (
string)c.Attribute("name"),
office = (
string)c.Parent.Attribute("name"),
party = (
string)c.Attribute("party"),
raised =
decimal.Parse(((string)c.Attribute("raised")).Replace("$", "").Replace(",", "")),
spent =
decimal.Parse(((string)c.Attribute("spent")).Replace("$", "").Replace(",", "")),
ind_supporting =
decimal.Parse(((string)c.Attribute("iefor")).Replace("$", "").Replace(",", "")),
ind_opposing =
decimal.Parse(((string)c.Attribute("ieagainst")).Replace("$", "").Replace(",", "")),
};
return View(q);
}
[
GridAction]
public ActionResult ajax_sw_candidates()
{
// read XML file to get data
doc =
XDocument.Load(@"C:\Workspace\Visual Studio 2008\Solutions\MvcQuerySystem\MvcQuerySystem\Content\xml\statewide2008.xml");
var q = from c in doc.Descendants("candidate")
select new can_sw_model
{
name = (
string)c.Attribute("name"),
office = (
string)c.Parent.Attribute("name"),
party = (
string)c.Attribute("party"),
raised =
decimal.Parse(((string)c.Attribute("raised")).Replace("$", "").Replace(",", "")),
spent =
decimal.Parse(((string)c.Attribute("spent")).Replace("$", "").Replace(",", "")),
ind_supporting =
decimal.Parse(((string)c.Attribute("iefor")).Replace("$", "").Replace(",", "")),
ind_opposing =
decimal.Parse(((string)c.Attribute("ieagainst")).Replace("$", "").Replace(",", "")),
};
return View(new GridModel
{
Data = q,
});
}
Without the DataBinding statement to use Ajax, the code works perfectly. When I add the DataBinding statement, after the first rendering, the currency fields lose their formatting. Any ideas (or solutions) would be highly appreciated.