开发者

conditionally display a link in a telerik grid using mvc3

I'm trying to add an action link to a grid. But only if a condition exists (the user is locked out). I can't get this to work in mvc3 (razor). Nothing is displayed.

I've tried:

    @Html.Telerik().Grid(Model.Users).Name("UserGrid").DataKeys(d开发者_如何学运维ataKeys => dataKeys.Add(o => o.UserName)).Columns(columns =>
   {
       columns.Template(s => Html.ActionLink(s.UserName, "Details", new { id = s.ProviderUserKey })).Title("Username (<i>click to edit</i>)");
        columns.Template(s => { if (s.IsLockedOut) Html.ActionLink("Unlock", "UnlockUser", new { username = s.UserName }, new { @class = "unlockimage" }); });
    }).Pageable().Sortable().Filterable()

and even when I remove the if(cond)... I can't get the actionlink to display. However if I don't use the lambda? it does work but is obviously displayed all the time.

        columns.Template(s => Html.ActionLink("Unlock", "UnlockUser", new { username = s.UserName }, new { @class = "unlockimage" }) );

any help is greatly appreciated.


Check this online demo how which shows how to use template columns in Razor. Here is an excerpt:

columns.Template(
                @<text>
                    <img 
                        alt="@item.CustomerID " 
                        src="@Url.Content("~/Content/Grid/Customers/" + item.CustomerID + ".jpg") " 
                      />
                </text>
            )


The solution from Rich above helped me craft a solution for needs. I have a single column that can display simple text, email address, or a url. I wanted to make the email address and url a selectable hot link. Below is an exert of my solution.

//Show the Value column. If the value is an email address of url display as a clickable hot link
    columns.Template(@<text> @if (item.ContactType.Value == "Email")
                               { 
                                   <a href="@Url.Content("mailto:" + item.DisplayValue)" >@item.DisplayValue</a>
                               } else if (item.ContactType.Value == "Website")
                               { 
                                   <a href="@Url.Content("http://" + item.DisplayValue)" >@item.DisplayValue</a>
                               } else
                               {
                                   @item.DisplayValue.ToString()
                               }
                      </text>)
            .Title("Value")
            .Width(250);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜