Hello Telerik Team,
I need to create a column in MVC 3 grid which is a button and I want it to redirect the user to an action called "edit " in the "home" controller
I tried this:
columns.Bound(c => c.Id).ClientTemplate(" <input type=\"button\" onclick=\"parent.location='@Url.Action(\"Edit\", \"Home\")'\" value=\"Edit\" />");
but it doesn't work , why ?
also, I need to pass the whole object(record) to the action , what can I do?
9 Answers, 1 is accepted
Does your code work outside of the grid? What is the value generated for the onlick attribute at runtime? If it does not you can try this:
onclick="location.href= "
instead of
onclick="parent.location"
You cannot pass the whole model to your action method - you can only pass a few properties:
'@Url.Action(\"Edit\", \"Home\", new { ModelProperty=\"<#= ModelProperty #>\" })'\"
Regards, Atanas Korchev
the Telerik team

but this also doesn't work
Which does not work? Could you be more specific? I need you to show us what the generated HTML is after binding a cell. If this is not possible you can provide a sample project which we can test.
Atanas Korchev
the Telerik team

the template I used columns.Bound(c => c.Id).ClientTemplate(" <input type=\"button\" onclick=\"location.href='@Url.Action(\"Edit\", \"Allergy\", new { id=\"<#= Id #>\" })'\" value=\"Edit\" />");<input id="38" onclick="location.href='@Url.Action(" type="button" })??="" {="" new="" ?Allergy?,="" Edit?,="" value="Edit"/>
the generated html is
It is obvious that it won't work because Url.Action is output without being executed. Url.Action is server side code and you need to execute it on the server. I suggest you check the client-templates online example which shows how to embed server side calls inside a client template. Here is an excerpt:
columns.Bound(c => c.CustomerID)The idea is to execute the server side code first and then use its value in the client-side template definition. Regards,
.ClientTemplate("<img alt='<#= CustomerID #>' src='"
+ Url.Content("~/Content/Grid/Customers/")
+ "<#= CustomerID #>.jpg' />")
.Title("Picture");
Atanas Korchev
the Telerik team

columns.Bound(user => user.Username).ClientTemplate("<#= <a href='" + Url.Action("EditUser", "CreateEditUser", new {username = user.Username}) + " '>Username</a> #>");
In this case, the anonymous type parameter to URL.Action seems that sets the value of username, is unable to be set, as there isnt an obvious way to escape to the context of the current row, and extract that value from user.
Is this possible?
As my college Atanas showed, you should use a slightly different syntax, similar to the following:
.ClientTemplate(
"<a href='"
+ Url.Action(
"EditUser"
,
"CreateEditUser"
,
new
{username =
"<#=Username#>"
}) +
" '>Username</a> "
)
Greetings,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

