web grid sorting not working mvc 3 razor - asp.net-mvc-3

I am using MVC 3 with Razor and I am using the below web grid to show some data,
What I need sorting on my first column. I have used similar code on other pages too for my sorting and it works fine, but here it doesnt seem to work.
However if I go to the next page say page 2 and now I click sort, it is sorted ascending and then again same problem.
<div id="grid">
#{
// added ajaxContainerId
var listgrid = new WebGrid(source: Model.ABC, rowsPerPage: 2, ajaxUpdateContainerId: "grid");
#listgrid.GetHtml(
columns: listgrid.Columns(
listgrid.Column("ColName", format: #<text>#item.Name</text>, canSort:true),
listgrid.Column(null, "Delete", (item) => MvcHtmlString.Create(string.Format("<a href='DeleteList/{0}'>Delete</a>", #item.Name))),
))
}
</div>

Full article at : http://yassershaikh.com/mvc-3-web-grid-sorting-not-working/
using the columnName attribute helped, I was using the wrong column name, because of which the sorting was not working
Here is the code I am using now
<div id="grid">
#{
// added ajaxContainerId
var listgrid = new WebGrid(source: Model.ABC, rowsPerPage: 2, ajaxUpdateContainerId: "grid");
#listgrid.GetHtml(
columns: listgrid.Columns(
listgrid.Column(header:"ColName", columnName="DbColName", format: #<text>#item.Name</text>, canSort:true),
listgrid.Column(null, "Delete", (item) => MvcHtmlString.Create(string.Format("<a href='DeleteList/{0}'>Delete</a>", #item.Name))),
))
}
</div>
Hope this helps someone in future too!

Related

WebGrid in Partial View - Stop paging from reloading page

I have a dashboard type page which contains multiple partial views each of which contain a webgrid.
For example this is web grids is in my _CurrentSubscriptions partial view:
WebGrid grid = new WebGrid(Model.Titles,
rowsPerPage: Model.PageSize,
defaultSort: "Name",
ajaxUpdateContainerId: "UserSubscriptions");
#grid.GetHtml(columns: grid.Columns(grid.Column("Name"), grid.Column("Description"),
grid.Column(format: #<text>#Html.ActionLink("Remove", "RemoveSub")</text>)));
I also have an _addSubscription partial view which contains the following grid to show search results.
WebGrid grid = new WebGrid(Model.Titles,
rowsPerPage: Model.PageSize,
defaultSort: "Name",
ajaxUpdateContainerId: "TitlesFound");
grid.Pager(WebGridPagerModes.All);
#grid.GetHtml(columns: grid.Columns(grid.Column("Name"), grid.Column("Description"),
grid.Column(format: #<text>#Html.ActionLink("Add", "AddSub")</text>)));
Both partial views are called from my Subscriptions/index.cshtml
Is it possible to restrict the paging on each of the grids from reloading the page and just update the selected grid?
Wrap your grid in a div with an Id.
(You have already done this) When you instantiate the grid, specify the AJAX parameter
ajaxUpdateContainerId with the div's Id.
Reload your page.
Click another page and notice there is no page post back; only the grid reloads.
<div id="UserSubscriptions">
#grid.GetHtml(columns:
grid.Columns(grid.Column("Name"),
grid.Column("Description"),
grid.Column(format: #<text>#Html.ActionLink("Add", "AddSub")</text>)));
</div>

Ajax WebGrid JQuery issue

I am using the webgrid so i can display data from my database. On page load the grid is displaying correctly on the page, but when i try to filter the data, i have a textbox and a submit button, when i press on the search button i get an error : "a jquery script reference is required in order to enable ajax support in the webgrid helper". I cant seem to find were the problem is.
this is my grid in my view,
Hello,
I am using the webgrid so i can display data from my database. On page load the grid is displaying correctly on the page, but when i try to filter the data, i have a textbox and a submit button, when i press on the search button i get an error : "a jquery script reference is required in order to enable ajax support in the webgrid helper". I cant seem to find were the problem is.
this is my grid in my view,
<div id="myGrid">
#Html.Partial("Grids/_gridPayments", Model)
</div>
this is my partial webgrid view
#model IEnumerable<DAS.DAL.CustomerPayment>
#{
var grid = new WebGrid(source: Model, rowsPerPage: 15, ajaxUpdateContainerId: "myGrid");
#grid.GetHtml(rowStyle: "gridRow", alternatingRowStyle: "gridAltRow", footerStyle: "gridFooter", columns: grid.Columns(
grid.Column("FullName", "Πελάτης"),
grid.Column("Amount", "Ποσό", format: (item) => string.Format("{0:C}", item.Amount)),
grid.Column("Descr", "Περιγραφή"),
grid.Column("DTPaid", "Ημερομηνία πληρωμής", format: (item) => string.Format("{0:dd-MMM-yyyy}", item.DTPaid))
));
}
and finally my controller function is
var payments = BLPayments.getAllPayments();
if (!string.IsNullOrEmpty(txtSearchCustomer))
payments = payments.Where(a => a.FullName.Contains(txtSearchCustomer)).ToList();
if (!string.IsNullOrEmpty(txtSearchFromDate) && !string.IsNullOrEmpty(txtSearchToDate))
payments = payments.Where(a => a.DTPaid >= DateTime.ParseExact(txtSearchFromDate, "MM/dd/yyyy", null) && a.DTPaid <= DateTime.ParseExact(txtSearchToDate, "MM/dd/yyyy", null)).ToList();
if (!string.IsNullOrEmpty(txtSearchFromDate) && string.IsNullOrEmpty(txtSearchToDate))
payments = payments.Where(a => a.DTPaid >= DateTime.ParseExact(txtSearchFromDate, "MM/dd/yyyy", null)).ToList();
if (string.IsNullOrEmpty(txtSearchFromDate) && !string.IsNullOrEmpty(txtSearchToDate))
payments = payments.Where(a => a.DTPaid <= DateTime.ParseExact(txtSearchToDate, "MM/dd/yyyy", null)).ToList();
return PartialView("Grids/_gridPayments", payments);
thanks in advance.
Added this code
if (Request.IsAjaxRequest())
return PartialView("Grids/_gridPremadeMeals", premadeMeals);
else
return View("ViewPremadeMeals", premadeMeals);
and worked. I think it was refreshing the whole page, but still cant find why..

How to get my Action method to give me back my model from web grid with checkboxes

I have a strongly typed view which is uses an IEnumerable as its model.
There's a bool property on the model which I'm rendering in a MVC WebGrid in a checkbox which the user can then check or uncheck.
On submit it gets to my action method but how do I get the checked checkboxes?
If I look at the Request.Params, I see all the checked checkboxes coming back as parameters. Is there any easy way for the DefaultModelBinder to convert these back into my IEnumerable, or do I have to do this by hand?
Here's my View so far:
#model IEnumerable<XXX.XXXItemDto>
<div id="items">
#using (Ajax.BeginForm("SaveEvents", "MyController", new AjaxOptions { UpdateTargetId = "items" }))
{
var grid = new WebGrid(Model, canPage: false, canSort: false);
#grid.GetHtml(columns: grid.Columns(
grid.Column("itemDescription", header: "Event"),
grid.Column("acknowledged", format: #<text><input name="Acknowledged_#(item.staffItemOid)" type="checkbox" value="#item.staffItemOid" #(item.acknowledged ? "Checked" : null)/> </text>, header: "Acknowledged")
))
#Html.SubmitButton("Save")
}
</div>

Adding custom html to Header in WebGrid

I'm trying to add a custom header for an MVC3 WebGrid.
The header property only allows for string, and any HTML is escaped.
My current grid razor view is as follows:
var grid = new WebGrid(Model, canPage: true, rowsPerPage: 5);
grid.Pager(WebGridPagerModes.NextPrevious);
#grid.GetHtml(tableStyle: "data_table-sorter",
alternatingRowStyle: "odd",
columns: grid.Columns(
grid.Column(header:"Select<span class=\"fi fi_gear\"></span>\"" , style: "table-select-col has-menu", canSort: false, format: #<input type="checkbox" value="#item.Id" />),
grid.Column("Name", "Briefing Book Name", canSort: true, style: "dj_sortable-table-column"),
grid.Column("Format", "Format", canSort: true, style: "dj_sortable-table-column")
));
How can I do this?
If you want to style individual headers in the current version of the WebGrid, you will have to use client-side code to do that.
for all header element....................
$("#gridContent").find('table thead tr a')
and then apply style any this u want see
.addClass()
.append()
for individual header.....................
actually this is applying style of rowcell to the headercell
var headerCells = $("#gridContent table tr th");
var firstRowCells = $("#gridContent table tr td");
$.each(firstRowCells, function (index, value) {
$(headerCells[index]).addClass($(firstRowCells[index]).attr("class"));
});
also see
MVC3 WebGrid Formatting or Styling Column Headers
How to add Html inside ASP.NET MVC 3 WebGrid Column Name (Header)
MVC3 WebGrid Formatting or Styling Column Headers

Does anyone have a working sample of json paging for mvc3 webgrid?

Does anyone have a working sample of json paging for mvc3 webgrid?
I've been trawling the interwebs for hours now looking for this and the best i can find is this link: Efficient Paging with WebGrid Web Helper - ASP.NET MVC 3 RC
I'm not convinced by the idea of writing the html in the controller though and I couldn't get the syntax right for creating edit/delete links.
Cheers!
So it turns out that most of the examples out there greatly overcomplicate matters.
A great example can be found here
It turns out the key is in the property ajaxUpdateContainerId which is grid in my case.
This wires up the grid to work without a full page refresh automagically.
I've posted some code from what I'm working on to provide the appropriate syntaxt.
#{
WebGrid webGrid = new WebGrid(canSort: false, canPage: true, rowsPerPage: 5, ajaxUpdateContainerId: "grid");
webGrid.Bind(Model, autoSortAndPage: false, rowCount: Model.TotalItemCount);
}
<div id="grid">
#webGrid.GetHtml(alternatingRowStyle: "altrow",
mode: WebGridPagerModes.All,
firstText: "<< first",
previousText: "< previous",
nextText: "next >",
lastText: "last >>",
columns: webGrid.Columns(
webGrid.Column("Name"),
webGrid.Column("State.Name", "State"),
webGrid.Column(header: "",
style: "action",
format: (item) => new HtmlString(Html.ActionLink("edit", "Edit", new { id = item.CityId }).ToString() + " | " +
Html.ActionLink("delete", "Delete", new { id = item.CityId }).ToString()
))))
</div>

Resources