How do I retrieve row ID from an MVCContrib HTML Grid? - asp.net-mvc

I currently have a product view page that contains an MVCContrib HTML Grid with a select link at the beginning of each row. If the select link is clicked, it takes me to a different page.
My question is whether it is possible to retrieve the productID from the row that is selected and pass that to the next page.
Maybe this is posible to do with a session variable but im not sure.
Any help would be greatly appreciated.
Thanks in advance.
Here is my view code:
<% Html.Grid((List<System2__MVC2.Controllers.ProductController.ProductsSet>)ViewData["Products"]).Columns(column =>
{
column.For(c => Html.ActionLink("Select", "Products", "Product")).DoNotEncode();
column.For(c => c.ProductID);
column.For(c => c.Name);
column.For(c => c.Description);
column.For(c => c.Price);
}).Render();
%>

Maybe I'm misunderstanding your question, but couldn't you just pass the ProductID as a route value to the ActionLink method? Something along the lines of:
Html.ActionLink("Select", "Products", "Product", new { ID = c.ProductID }, null)

Related

How to create a hyperlink using the id of an item in a client template on Kendo Grid

So I have a Kendo Grid using the 'Hierarchical' mechanism. When I come to my Client Template portion, I'm doing this:
.Columns(column =>
{
column.Bound(o => o.Id).Width(110);
column.Bound(o => o.Title);
column.Bound(model => model.Id)
.ClientTemplate(String.Format("<i class=\"glyphicon glyph-btn directory\">Stuff</i>", Url.Action("Details", "Stuff", new { Id = "#=Id#" })))
.Width(40)
.Title("View");
})
When I run this up, the Id (on the first column) is coming out correctly - identifying the group item by its correct Id. However, when I try to use "#=Id#" in my hyperlink, this always refers to the Id of the original item that I clicked to show the group (client template).
Does anyone know how to use the Id of the group item (i.e. the output of that first column) in the hyperlink?
Thanks in advance
If you look at the answer to this telerik question:
You need to use \\#Id\\# within your ChildTemplate method to indicate you are accessing the current child element, not the parent. Change your syntax to the following:
.ClientTemplate(String.Format("<i class=\"glyphicon glyph-btn directory\">Stuff</i>", Url.Action("Details", "Stuff", new { Id = "\\#=Id\\#" })))

Telerik Tabstrip not displaying inside Telerik Grid

I have a Telerik MVC grid with a single detail view.
Inside the detail view is a telerik tabstrip. When I select the grid row to display, the detail will open up completely empty.
I have copied the tabstrip code outside the grid, changing the name of the grid to a static name and rendering the grid instead of outputting it as an htmlstring, and it displays perfectly.
I've tried registering all required javascript files, no change.
I'm pretty stumped at this point. I've cleared out the entire tabstrip so it contains nothing but text, but no change.
Here's the code which i have:
<%: Html.Telerik().Grid(Model.AccessRequests)
.Name("gridAccessRequests")
.ColumnContextMenu()
.DataKeys(d => { d.Add(a => a.Access_Request_ID).RouteKey("id");})
.DataBinding(dataBinding => dataBinding
.Ajax()
.Select("AjaxBinding", "Home")
.Insert("Insert", "Home")
.Update("Save", "Home")
.Delete("Delete", "Home"))
.ToolBar(commands => commands.Insert())
.Editable(editing => editing
.InsertRowPosition(GridInsertRowPosition.Bottom)
.Mode(GridEditMode.PopUp))
.HtmlAttributes(new { style = "width:100%" })
.Selectable()
.Sortable(sorting => sorting
.OrderBy(sortOrder => sortOrder.Add(o => o.EMPLOYEE_NAME).Ascending()))
.Filterable(filtering => filtering
.Filters(filters => filters
.Add(o => o.RECORD_YEAR).IsEqualTo(Model.CurrentYear)))
.Pageable(paging =>
paging.PageSize(15)
.Style(GridPagerStyles.NextPreviousAndNumeric)
.Position(GridPagerPosition.Both))
.ClientEvents(events =>
{
events.OnEdit("onEdit");
events.OnRowDataBound("requests_onRowDataBound");
})
.DetailView(details => details.ClientTemplate(
Html.Telerik()
.TabStrip()
.Name("TabStrip_<#=id#>")
.SelectedIndex(0)
.Items(items =>
{
items.Add().Text("Assets").Content("Assets");
items.Add().Text("Locations").Content("Locations");
})
.ToHtmlString()
))
.Columns(col =>
{
col.Bound(c => c.Access_Request_ID).Title("ID");
col.Bound(c => c.RECORD_YEAR).Title("Year");
col.Bound(c => c.VERSION_NO).Title("Version");
col.Bound(c => c.EMPLOYEE_NAME).Title("Name");
col.Command(commands =>
{
commands.Edit()
.ButtonType(GridButtonType.Image);
commands.Delete()
.ButtonType(GridButtonType.Image);
commands.Custom("Clone")// the constructor accepts the name of the custom command (used to differentiate between multiple custom commands)
.Text("Clone")
// Specify which fields from the bound data item to include in the route (pass as action method arguments)
.DataRouteValues(route => route.Add(o => o.Access_Request_ID).RouteKey("requestId"))
.Ajax(true)
// Which action method to call
.Action("CloneRequest", "Home");
}).Width(145)
.IncludeInContextMenu(false);
})%>
Here is the standalone tabstrip:
<%
Html.Telerik().TabStrip()
// Make the Name unique
.Name("TabStrip")
.SelectedIndex(0)
.Items(items =>
{
items.Add().Text("Assets").Content("Assets");
items.Add().Text("Locations").Content("Locations");
})
.Render();
%>
Any Suggestion/ Solution/Demo will be helpful.
Thanks in advance!
UPDATE
I have figured out that the tabstrips are not displaying on initial load of the grid; after hitting the grid's refresh button or paging the tabstrips display and function correctly.
It appears that there is an invalid property value error being thrown after the grid is loaded but before any databinding is completed, but I still can't seem to pinpoint the exact problem. Again, hitting the grid's refresh button or paging will successfully databind all the rows.
When the page first loads, the grid is loaded via server side binding. The tab strips are defined in the client template which is not used during server binding. When you refresh the grid, it is loaded via ajax binding and the client template is used.
You need to make the grid load initially via ajax binding so you need to use a different constructor.
Change this:
Html.Telerik().Grid(Model.AccessRequests)
to this:
Html.Telerik().Grid<AccessRequests>() //Put in the appropriate type for AccessRequests

Infragistics hidden column update

I'm using Infragistics on an Entity Framework code-first MVC project. I want to display a table with a hidden column (the ID) and it has to be editable. Here is what I've got so far:
<table id="igTests"></table>
#(Html.Infragistics().Grid<BusinessModel.VO.TestVO>().ID("igTests")
.AutoGenerateColumns(false)
.Columns(column =>
{
column.For(x => x.TestId).DataType("int").HeaderText("id");
column.For(x => x.TestNum).DataType("int").HeaderText("Test num");
column.For(x => x.Type).DataType("string").HeaderText("Type");
column.For(x => x.Nature).DataType("string").HeaderText("Nature");
column.For(x => x.TeamName).DataType("string").HeaderText("Team");
column.For(x => x.CreateDate).DataType("date").HeaderText("Creation date");
})
.Features(feature => {
feature.Sorting().CaseSensitive(true);
feature.Filtering().Mode(FilterMode.Simple);
})
.PrimaryKey("TestId")
.DataSource(Model.TestsVO.AsQueryable())
.DataBind()
.Render())
This is what is displayed:
Now lets add the update feature (i know the readOnly is useless since we are not supposed to see it):
feature.Updating().EnableAddRow(true).EnableDeleteRow(true).EditMode(GridEditMode.Row).ColumnSettings(settings =>
settings.ColumnSetting().ColumnKey("TestId").ReadOnly(true)
);
And hide my ID-column:
column.For(x => x.TestId).DataType("int").HeaderText("id").Hidden(true);
Here is what i get. As you can see the table acts like my ID-column was visible.
This happened when I added the update feature. Do you have any idea on how I could fix the "Add new row" row acting like my column was visible? Thanks in advance.
Probably you should add this
}).Features(features => features.Hiding()
.ColumnSettings(settings =>
{
settings.ColumnSetting().ColumnKey("id").Hidden(true).AllowHiding(false)
http://www.infragistics.com/products/jquery/sample/grid/column-hiding-on-initialization
I got it to work using .Width("0px") and ReadOnly on my ID-column. A bit dirty but had no other choice...

Telerik MVC Grid won't load data into details table (subtable)

I have a list of Plants and assosiated Projects. I want to output this in a table with all the Plants and use Telerik.Grid to expand a Plant, show a Telerik.Grid with associated Projects. I want the Projects to be dynamically loaded with Ajax.
The code for the grid:
#(Html.Telerik().Grid<PlantDto>()
.Name("Plants")
.Columns(columns =>
{
columns.Bound(plant => plant.Title);
})
.DetailView(details => details.ClientTemplate(
Html.Telerik().Grid<ProjectDto>()
.Name("Plant_<#= Id #>")
.DataBinding(dataBinding => dataBinding.Ajax()
.Select("ProjectsForPlant", "User", new { plantId = "<#= Id #>" }))
.ToHtmlString()
))
.DataBinding(dataBinding => dataBinding.Ajax().Select("PlantsForUser", "User"))
)
The initial data is loaded into the grid just fine (the list of Plants) but when I expand a plant I just get an empty sub-table.
Looking in FireBug there are no calls to the server. The controller that should serve the list of projects is never called.
Anyone have an idea on what it could be?
Update: Looks like what was causing trouble was that the plant.id had an "$" in it. Like "PCD$ODIN". Looks like that made life difficult for the JavaScript.
I compared your configuration to the one here and it looks identical. Test whether this Select method:
.Select("ProjectsForPlant", "User", new { plantId = "<#= Id #>" }))
set the plant id properly and if you need to name it exactly the same as the Id field instead of plantId, i.e.:
.Select("ProjectsForPlant", "User", new { Id = "<#= Id #>" }))

Format boolean column in MVCContrib grid

I am using the MVCContrib grid and I would like to display images depending on the values of a column, e.g.:
if the column's value is null display the image "<img src="true.gif">"
otherwise display the image "<img src="false.gif">
Furthermore I would also need (this should be the same approeach I think) to display different actions depending on the column's/rows' value ...
Thanks in advance for your answers!
Best regards
Stefan
The answer was quite simple :)
<% Html.Grid(Model.Languages).Columns(column =>
{
column.For(c => c.LanguageName).Named("Language");
column.For(c => c.LangCode).Named("Language Code");
column.For(c => c.IsDefaultLanguage ? "<img src=\"library/images/true.gif\">" : "<img src=\"library/images/false.gif\">").Named("Default Language").DoNotEncode();
}
).Empty("There is no language available")
.Render(); %>

Resources