joeharrison714 / MVCGrid.Net

http://mvcgrid.net
MIT License
74 stars 55 forks source link

Conditional .WithValueTemplate in MVCGrid #104

Open yunusrf opened 7 years ago

yunusrf commented 7 years ago

Hi

I am using the MVCGrid.Net and have issues in adding a column with shows a link to Edit depending on some condition ( For ex : if the value of item in the record is "CanEdit" show link or else do not show the link). Please let us know the sample code to achieve this.

Thanks and kind regards Yunus

madzman23 commented 7 years ago

Hi, Does anyone have any idea how can we do this? @yunusrf did you get any solution?

Thanks, Match

yunusrf commented 7 years ago

Hi @madzman23 ,

Please use StringBuilder to create the required link.

See the below sample text.Hope this help. Thank you.


cols.Add("Action")
       .WithHeaderText("Action")
       .WithSorting(true)
       .WithVisibility(visible: true, allowChangeVisibility: true)
       .WithHtmlEncoding(false)
       .WithCellCssClassExpression(m => "text-center")
       .WithValueExpression((p, c) =>
       {

        // build whatever html you want
          StringBuilder sb = new StringBuilder();
           if (p.bAccessParam == true)
           {

               string urlDisable = c.UrlHelper.Action("DisableIndicator", "User", new { recordID = p.recordID });
               string href = " href= " + urlDisable + " class=approve >";
               sb.Append(" <a  ");
               //  sb.Append(target);
               sb.Append(href);
               sb.Append(p.enableDisable);
               sb.Append("</a> ");

           }
           else
           {
               string urlEnable = c.UrlHelper.Action("EnableIndicator", "User", new { recordID = p.recordID });
               string href = " href= " + urlEnable + " class=approve >";
               sb.Append(" <a  ");
               //  sb.Append(target);
               sb.Append(href);
               sb.Append(p.enableDisable);
               sb.Append("</a> ");
           }

           return sb.ToString();
       });

madzman23 commented 7 years ago

It works. :tada: Thank you mate, I have been scratching my head editing the MVCGrid codes for days. :rofl: Cheers!