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

[Solved] Conditional use of ActionLink helpers in client templates

5 Answers 378 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Pablo
Top achievements
Rank 1
Pablo asked on 07 Nov 2011, 06:15 PM
Hi,

I need to use the Ajax.ActionLink helper inside a ClientTemplate for a column in a Telerik MVC Grid, but only if certain condition is true.

This is the code I have used, but it's generating only text in the column, not the link for the action.

.ClientTemplate(@" <#= !CheckListForThisYearSubmitted?" +
                            Ajax.ActionLink("Unsubmit checklist""SubmitCheckList"new { enable = false, id = 1 }, new AjaxOptions { UpdateTargetId = "generalDivTest" }).ToString() + ":'' #> ")

If i don't use the conditional sentence, the link is always been shown and it does work fine, but i need that logic to show or hide the link for the action.

What am i doing wrong ?

Here is the full definition of the grid, just in case:
@(Html.Telerik().Grid(Model)
                    .Name("GridPersonBusiness")
                    .DataKeys(dataKeys => dataKeys.Add(c => c.ID))
                    .Columns(columns =>
                    {
                        columns.Bound(o => o.Name);
                        columns.Bound(o => o.UTR);
                        columns.Bound(o => o.TypeOfClient).Width(160);
                        columns.Bound(o => o.HasDataFromPreviousYears).Title("Data previous years")
                            .ClientTemplate(@" <input type=""checkbox"" <#= HasDataFromPreviousYears?'checked=""true""':'' #> disabled=""disabled"" />").Width(160);
                        columns.Template(
                        @<text>
                                <input name="checkedRecords" type="checkbox" value="@item.ID" title="checkedRecords"
                                @if (item.CheckListForThisYearCreated)
                                {
                                    <text>disabled="disabled"</text>
                                }
                            />
                                </text>).ClientTemplate(@" <input name=""checkedRecords"" type=""checkbox"" value=<#= ID #> title=""checkedRecords"" <#= CheckListForThisYearCreated? disabled='disabled' : '' #> />")
                        .Title("Create checkList").Width(160);
 
                        columns.Template(
                        @<text>
                                @if (item.CheckListForThisYearSubmitted)
                                {
                                    <text>CheckList submitted - </text>
@*                                    @Html.ActionLink("Unsubmit", "SubmitCheckList", new { id = item.ID, enable = "false" })*@
                                    @Ajax.ActionLink("Unsubmit", "SubmitCheckList", new { id = item.ID, enable = "false" }, new AjaxOptions { UpdateTargetId = "generalDivTest" })
                                }
                                else if (item.CheckListForThisYearCreated)
                                {
        @*                                    <text><a href="/CheckList/FillOnlineForm/@item.ID" >Online checklist</a></text>
                                    <text><a href="/CheckList/GetHardCopy/@item.ID">Hardcopy checklist</a></text>
                                    @Html.ActionLink("Delete checklist", "DeleteCheckList", new { id = item.ID })*@
                                    @Ajax.ActionLink("Fill Online Checklist NEW", "FillOnlineForm", new { id = item.ID }, new AjaxOptions { UpdateTargetId = "generalDivTest" })
                                    @Ajax.ActionLink("Hardcopy Checklist NEW", "GetHardCopy", new { id = item.ID }, new AjaxOptions { UpdateTargetId = "generalDivTest" })
                                    @Ajax.ActionLink("Delete checklist NEW", "DeleteCheckList", new { id = item.ID }, new AjaxOptions { UpdateTargetId = "generalDivTest" })
                                }
                                </text>)
 
                            .ClientTemplate(@" <#= !CheckListForThisYearSubmitted?" +
                            Ajax.ActionLink("Unsubmit checklist", "SubmitCheckList", new { enable = false, id = 1 }, new AjaxOptions { UpdateTargetId = "generalDivTest" }).ToString() + ":'' #> ")
                        .Title("Submission");
 
                    })
                                            .DataBinding(dataBinding =>
                                            {
                                                dataBinding.Ajax().Select("_AjaxGetCheckLists", "CheckList");
                                            })
 
                                            .Sortable()
                                            .Selectable()
                                            .Pageable(paging => paging.Style(GridPagerStyles.Status).PageOnScroll(true))//.Pageable(a => a.PageSize(10))
                                            .Filterable()
                                            .Footer(true));


Thanks in advance

5 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 08 Nov 2011, 10:10 AM
Hello Pablo,

In order this to work there are various characters which should be escaped. Also note that the whole expression should be inform of a string:

"<#=
CheckListForThisYearSubmitted ? '" + @Ajax.ActionLink("Unsubmit checklist", "SubmitCheckList", new { enable = false, id = 1 }, new AjaxOptions { UpdateTargetId = "generalDivTest" }).ToHtmlString().Replace("&#39;", "\"") + "': '' #>");

Greetings,
Rosen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Pablo
Top achievements
Rank 1
answered on 08 Nov 2011, 11:27 AM
Hi Rosen,

sorry but that hasn't solved the problem. The markup that is being generated is just the string which was given as the first argument of the helper. I have tried all of this:                          

.ClientTemplate(@" <#= !CheckListForThisYearSubmitted?" +
                            Ajax.ActionLink("Unsubmit checklist", "SubmitCheckList", new { enable = false, id = 1 }, new AjaxOptions { UpdateTargetId = "generalDivTest" }).ToHtmlString().Replace("&#39", "\"") + ":'' #> ")                           
                             
.ClientTemplate(@" <#= !CheckListForThisYearSubmitted?" +
                            Ajax.ActionLink("Unsubmit checklist", "SubmitCheckList", new { enable = false, id = 1 }, new AjaxOptions { UpdateTargetId = "generalDivTest" }).ToHtmlString().Replace("&#39;", "\"") + ":'' #> ")     
 
.ClientTemplate(@" <#= !CheckListForThisYearSubmitted?" +
                            @Ajax.ActionLink("Unsubmit checklist", "SubmitCheckList", new { enable = false, id = 1 }, new AjaxOptions { UpdateTargetId = "generalDivTest" }).ToHtmlString().Replace("&#39", "\"") + ":'' #> ")     


what am i missing?
0
Rosen
Telerik team
answered on 08 Nov 2011, 01:48 PM
Hello Pablo,

Actually you are missing the single quotes:

"<#=!CheckListForThisYearSubmitted ? '" + @Ajax.ActionLink("Unsubmit checklist", "SubmitCheckList", new { enable = false, id = 1 }, new AjaxOptions { UpdateTargetId = "generalDivTest" }).ToHtmlString().Replace("&#39", "\"") + "': '' #>"


Regards,
Rosen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Pablo
Top achievements
Rank 1
answered on 08 Nov 2011, 01:55 PM
Hi,

in the beginning i was using the single quotes but i was getting this error in the js console:

illegal character
$(d).data("tGrid") is undefined

Without the quotes this error doesn't come up but it's still not working and i'm just getting a string without the link for the action...

any ideas?
0
Rosen
Telerik team
answered on 09 Nov 2011, 08:39 AM
Hi Pablo,

Could you please send us a small sample in which this error can be observed. This way we will be able to get better understanding about your scenario and provide a more to-the-point answer.

Greetings,
Rosen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
Tags
Grid
Asked by
Pablo
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Pablo
Top achievements
Rank 1
Share this question
or