Hello,
I'm trying to have a column with a template to display a link.
The following template works: "<a href='/DevOps/SoftwareInformation/Filter/${Name}'>${Name}</a>"However, I would like to create a Html.ActionLink to directly link to the controller & action.
In my cshtml file this is how I would do it:@Html.ActionLink(linkText:"Test Filter", actionName:"Filter", controllerName:"SoftwareInformation", routeValues:new { id="Test"}, htmlAttributes:null)
However, I don't manage to translate this to a column template. Everything I try just gets translated to pure text.
This is the code I use to define the column:
model.Columns.Add(new GridColumn("Name", "Name", "string", "220px") { Template = @"Html.ActionLink(linkText:""${Name} Filter"", actionName:""Filter"", controllerName:""SoftwareInformation"", routeValues:new { id=""${Name}""}, htmlAttributes:null)"});
Any advice ?
Kind regards,
Michael
Hello Michael,
Thank you for posting in our forum.
The Template for the column you set needs to be a string. What Html.ActionLink ultimately renders on a page is html so once you define the action link you can call its ToHtmlString() method and pass the result to the Template for the column. For example:
col.For(x => x.Name).Template(@Html.ActionLink(linkText: "${Name}",actionName: "Index",controllerName:"Home").ToHtmlString());
Let me know if you have any questions.
Best Regards,
Maya Kirova
Developer Support Engineer II
Infragistics, Inc.
http://es.infragistics.com/support
Hello Maya,
Thanks for the example. However I am using C# code to set the template of a column on the GridModel and not doing it in the cshtml file. Do you have example code for that aswell ?
var model = new GridModel();model.Columns.Add(new GridColumn("Name", "Name", "string", "220px") { Template = """});
Hello Michael ,
You can refer to the following thread on how you can call the Html.ActionLink method inside your controller:
http://stackoverflow.com/questions/8331041/how-to-use-mvc-3-html-actionlink-inside-c-sharp-code
Basically once you create a HtmlHelper you can then use it to create the action link form the LinkExtensions class:
var html = new HtmlHelper(new ViewContext(controllerContext, view, vdd, tdd, new StringWriter()),
new ViewDataContainer(vdd), RouteTable.Routes);
var htmlAnchor = LinkExtensions.ActionLink(html, "Link Text", "ActionName", "Controller").ToHtmlString();
You could build the action link similarly to the one I suggested in the previous post and then pass its html string as the template of the column in your code.
Thanks a lot for this, it works great.
One small addition I want to make: if I want to pass a routvalue to the actionlink which contains the column "${Name}" then the LinkExtensions.ActionLink encodes the string which breaks the grid functionality. This is only done on the routvalues and not on the linktext.resulting template: "<a href=\"/x/y/SoftwareFilter/%24%7BName%7D\">${Name}</a>"
I've avoided this problem by replacing that in the string created by the .ToHtmlString()
Final result:
var template = Helper.Html(Controller).ActionLink("${Name}", "SoftwareFilter", "y", new { id = "replace" }, null).ToHtmlString();column.Template = template.Replace("replace", "${Name}")