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. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

DropDownListFor html helper extension

Thread ID:

Created:

Updated:

Platform:

Replies:

126093 Sep 23,2016 08:13 AM UTC Sep 28,2016 05:24 AM UTC ASP.NET MVC 3
loading
Tags: DropDownList
Tom Frajer
Asked On September 23, 2016 08:13 AM UTC

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

    }
}

Dhivyalakshmi Thirumurugan [Syncfusion]
Replied On 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. 


Tom Frajer
Replied On 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.

Dhivyalakshmi Thirumurugan [Syncfusion]
Replied On 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.  


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;