Question

I've been trying to implement the code in the answer here

Issues are:-

  1. Setting up or creation of helper
  2. integrating helper onto view to perform CascadingDropDropListFor

At 1st I tried making a new class for the helper then I instead moved the code into my existing model for the page but found if I did that it solved the issue 2 just to say I couldn't nest the public static class into my public class StudentViewModel.

To solve issue 1 I tried inputing as many "Using" as needed to get all the code to work and then I found that Web.Mvc.Html was meant to resolve it but didn't and caused another error.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

I did find that

using System.Web.WebPages.Html;

Solved the Html.SelectExtension issue which you can see the error below

The name 'Html' does not exist in the current context

But caused

'SelectListItem' is an ambiguous reference between 'System.Web.Mvc.SelectListItem' and 'System.Web.WebPages.Html.SelectListItem'

Below you can see the helper code which was suggested but I think I must be doing something wrong at some stage to keep causing these different errors to pop up.

            public static class MvcHtmlExtensions
    {
        public static MvcHtmlString CascadingDropDownListFor<TModel, TProperty>(
            this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression,
            IEnumerable<SelectListItem> selectList,
            string optionLabel,
            IDictionary<string, Object> htmlAttributes,
            string parentControlName,
            string childListUrl
            )
        {
            var memberName = GetMemberInfo(expression).Member.Name;
            MvcHtmlString returnHtml = Html.SelectExtensions.DropDownListFor(htmlHelper, expression, selectList, optionLabel, htmlAttributes);
            var returnString = MvcHtmlString.Create(returnHtml.ToString() +
                @"<script type=""text/javascript"">
                    $(document).ready(function () { 
                        $(""#<<parentControlName>>"").change(function () {                                  
                            var postData = { <<parentControlName>>: $(""#<<parentControlName>>"").val() };                                 
                            $.post('<<childListUrl>>', postData, function (data) {                                     
                                var options = """";                                     
                                $.each(data, function (index) {                                         
                                    options += ""<option value='"" + data[index].Id + ""'>"" + data[index].Name + ""</option>"";                                     
                                });                                     
                                $(""#<<memberName>>"").html(options);                                 
                            })                                 
                            .error(function (jqXHR, textStatus, errorThrown) { alert(jqXHR.responseText); });                             
                        });                         
                    });                      
                </script>"
                .Replace("<<parentControlName>>", parentControlName)
                .Replace("<<childListUrl>>", childListUrl)
                .Replace("<<memberName>>", memberName));
            return returnString;
        }
        private static MemberExpression GetMemberInfo(Expression method)
        {
            LambdaExpression lambda = method as LambdaExpression;
            if (lambda == null)
                throw new ArgumentNullException("method");
            MemberExpression memberExpr = null;
            if (lambda.Body.NodeType == ExpressionType.Convert)
            {
                memberExpr = ((UnaryExpression)lambda.Body).Operand as MemberExpression;
            }
            else if (lambda.Body.NodeType == ExpressionType.MemberAccess)
            {
                memberExpr = lambda.Body as MemberExpression;
            }
            if (memberExpr == null)
                throw new ArgumentException("method");
            return memberExpr;
        }
    }
}

Otherwise if anybody knowns of a much simpler way of implementing cascading dropdown lists in mvc3 I'll try that out instead but I've already been looking at a bunch of other search results for it and this had been the one I had figured easiest to implement.

Anyway any help would be much appreciated.

Was it helpful?

Solution

use the AjaxDropdown from http://awesome.codeplex.com and you won't need to write javascript anymore

OTHER TIPS

Use

System.Web.Mvc.Html.SelectExtensions.DropDownListFor(..);

instead of

using System.Web.WebPages.Html;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top