This is a migrated thread and some comments may be shown as answers.

client template Razor

9 Answers 517 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Nebras
Top achievements
Rank 1
Nebras asked on 19 Jun 2011, 05:37 PM
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

Sort by
0
Atanas Korchev
Telerik team
answered on 20 Jun 2011, 07:49 AM
Hi Nebras ,

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
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
0
Nebras
Top achievements
Rank 1
answered on 20 Jun 2011, 11:53 AM
Thanks Atanas  for your  reply,
but this also doesn't work 
0
Atanas Korchev
Telerik team
answered on 20 Jun 2011, 11:54 AM
Hello Nebras ,

 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.

Regards,
Atanas Korchev
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
0
Nebras
Top achievements
Rank 1
answered on 20 Jun 2011, 12:50 PM
sorry for confusion ,
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\" />");

the generated html is
 <input id="38" onclick="location.href='@Url.Action(" type="button" })??="" {="" new="" ?Allergy?,="" Edit?,="" value="Edit"/>
0
Atanas Korchev
Telerik team
answered on 20 Jun 2011, 03:48 PM
Hi Nebras ,

 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)
                .ClientTemplate("<img alt='<#= CustomerID #>' src='"
                    + Url.Content("~/Content/Grid/Customers/")
                    + "<#= CustomerID #>.jpg' />")
                .Title("Picture");
The idea is to execute the server side code first and then use its value in the client-side template definition.

Regards,
Atanas Korchev
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
0
Kamau
Top achievements
Rank 1
answered on 26 Jul 2011, 09:30 PM
How does this work when using URL.Action? For instance:
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?
0
Rosen
Telerik team
answered on 27 Jul 2011, 08:26 AM
Hi Kamau,

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

0
yashwanth krishna
Top achievements
Rank 1
answered on 03 Oct 2011, 09:09 AM
I wanted to place an datepicker in the grid Using Editor Template..Is it possible and How can i place it??
0
Keerthi
Top achievements
Rank 1
answered on 16 Oct 2012, 04:31 AM
Parameter that I'm passing in the Url has special characters. How to encode the parameter?
Tags
General Discussions
Asked by
Nebras
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Nebras
Top achievements
Rank 1
Kamau
Top achievements
Rank 1
Rosen
Telerik team
yashwanth krishna
Top achievements
Rank 1
Keerthi
Top achievements
Rank 1
Share this question
or