3
votes

I am working with ASP.NET MVC 4 with Kendo UI(kendo grid).Below is sample code of Kendo Grid -

@(Html.Kendo().Grid(Model.Users).Name("Grid").Columns(columns =>
{
   columns.Bound(p => p.FirstName);
   columns.Bound(p => p.LastName);
   columns.Bound(p => p.UserName);
   columns.Bound(p => p.Email);   
   columns.Bound(o => o.IsActive).ClientTemplate(links).Title("Action");      

})

In the above code my IsActive column have some links for Actions like Edit,Update,Delete.And i am adding those links into Kendo grid by links variable.And I want to use links variable on the basis of conditions.Means i want conditional ClientTemplate here.

So anyone suggest how can make a conditional ClientTemplate in kendoGrid ?

2) Also i want to add condition on the basis on the bool field value of my model(Model.Users).

So i want to know how we can get that field from Model.Users model in kendo grid for each row.Like -

.ClientTemplate(if(IsAdmin && ViewBag.IsActive){.....} else{....})
3

3 Answers

3
votes

You can try like below code..may be this help you..

columns.Bound(p => p.Active).ClientTemplate("\\#if('#=Active#'=='Y') {\\<input type='button' value='OK' />\\}\\#");

or may be use

"#= (Active) ? ' ' : 'your code here' #"
1
votes

You can use the following piece of code:

@(Html.Kendo().Grid(Model.Users).Name("Grid").Columns(columns =>
{
  columns.Bound(p => p.FirstName);
  columns.Bound(p => p.LastName);
  columns.Bound(p => p.UserName);
  columns.Bound(p => p.Email);   
  columns.Bound(o => o.IsActive).ClientTemplate("#if(IsActive){#<a href='javascript:void(0)' >Edit</a>#}#").Title("Action");

})
0
votes

I'm concatenating a name and using a javascript function which made condition testing much easier, plus you can get access to multiple fields:

cshtml:
@(Html.Kendo().Grid<Debtors>()
    .Name("Debtors")
    .Columns(columns =>
    {
        columns.Bound(c => c).Title("Name").ClientTemplate("#=showName(data)#");
        columns.Bound(c => c.Busname);
        ...
     })
    ...
)

js:
function showName(data) {
    var returnName = "";
    if (data.Lname) {
        returnName = data.Lname;
        if (data.Fname) {
            returnName += ", " + data.Fname;
            if (data.Mi) {
                returnName += " " + data.Mi;
            }
        }
    }
    return returnName;
}