We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

DropDownListFor html helper extension

Hi,
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);
        }

    }
}

3 Replies

DT Dhivyalakshmi Thirumurugan Syncfusion Team September 27, 2016 12:05 PM UTC

Hi Tom, 

Thanks for contacting Syncfusion support. 

We can extend the DropDownListFor helper as per your requirement. Please find the code below. 

public static class Extension 
    { 
        static Extension() 
        { 
            EssentialJavaScript.MetadataProvider = new MvcModelMetadataProvider(); 
        } 

        // To create helper using extension method 

        public static DropDownListPropertiesBuilder MyDDL<TModel, TValue>(this EssentialJavaScriptFor<TModel> html, Expression<Func<TModel, TValue>> expression, DropDownListProperties model) 
        { 
            return DropDownListExtenaion.DropDownListFor(html, expression, model, null, null); 

        } 
    } 

@Html.EJ().MyDDL(Model => Model.DropData, (Syncfusion.JavaScript.Models.DropDownListProperties)ViewData["properties"]).SelectedIndex(0) 

We have prepared the sample for your reference and it can be downloaded from the below link. 


Note: The newly created helper will come under the EJ() extension as shown above, because we have written our control source within EJ() extension class. 

Please let us know if you have any queries. 

Regards, 
Dhivyalakshmi. 



TF Tom Frajer September 27, 2016 01:01 PM UTC

Thank you very much - I was very near, but missed some things in my implementation :)

I noticed that you have misstyping here: DropDownListExtenaion - not a big concern, but you should fix it in some of the new versions.


DT Dhivyalakshmi Thirumurugan Syncfusion Team September 28, 2016 05:24 AM UTC

Hi Tom, 

Thanks for the update. 

We are glad to hear that the reported requirement has been achieved at your end. And as per your concern we will change the DropDownListExtenaion class name and the changes will be available on our 2016 Volume 3 SP1 release. Thanks for your concern once again. 

Please let us now if you need any further assistance. 

Regards, 
Dhivyalakshmi.  


Loader.
Live Chat Icon For mobile
Up arrow icon