I need to write extension for DropDownListFor helper without completely rewriting it. I want to use all available functionality and only add my own logic for checking user permission rights. I already extended default MVC TextBox - this war relatively easy - but I do not know how to extend Syncfusion heplers. Here is what I wrote for my textbox:
using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using CarDat.Service;
namespace CarDat.Helpers
{
public static class TextBoxExtensions
{
public static MvcHtmlString LabeledTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string label)
{
// Set class attribute for css styling
var attributes = new RouteValueDictionary();
attributes.Add("class", "e-textbox");
// Get field name
var body = expression.Body as MemberExpression;
var fieldName = body.Member.Name;
// Initialize new service for retrieving user rights
var permissionService = new PermissionService();
// Get field read permissions for current user role
var canRead = permissionService.HasFieldReadPermission(fieldName);
// Get field read permissions for current user role
var canEdit = permissionService.HasFieldWritePermission(fieldName);
// Render HTML code for label and input field
var html = "";
if (canRead || canEdit) {
if (!canEdit) {
attributes.Add("readonly", "readonly");
}
html = htmlHelper.Label(label).ToString();
html += InputExtensions.TextBoxFor(htmlHelper, expression, attributes).ToString();
}
return new MvcHtmlString(html);
}
}
}