就此问题设立的MVC级联下拉菜单 [英] Issue setting up Cascading drop downs in MVC

查看:165
本文介绍了就此问题设立的MVC级联下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在这里实现了答案的code

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

问题是: -


  1. 设置或辅助的创建

  2. 集成帮手到视图执行CascadingDropDropListFor

在第一次我试图使一个新的类的助手,那么我而不是移动code到我现有的页面模型,但发现,如果我这样做,它解决了问题2只是说我不能窝公共静态类在我的公共类StudentViewModel。

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.

要解决问题1我试着输入查询尽可能多的使用按需获取所有code工作,然后我发现Web.Mvc.Html是为了解决这个问题,但没有造成其他错误。

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;

我确实发现

using System.Web.WebPages.Html;

解决了的 HTML 的.SelectExtension问题,你可以看到下面

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

名字'HTML'不存在于当前上下文存在

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

但引起

SelectListItem'之间不明确的引用
  System.Web.Mvc.SelectListItem和
  System.Web.WebPages.Html.SelectListItem

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

下面你可以看到帮手code这是建议,但我认为我必须做一些错误的在某些阶段,以保持引起这些不同的错误弹出。

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

否则,如果在实施中MVC3级联下拉列表的一个更简单的方法任何人的已知,我会尝试了这一点,而不是,但我已经在看一堆它其他的搜索结果,这一直是一个我有想通最容易实现的。

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.

反正任何帮助将非常AP preciated。

Anyway any help would be much appreciated.

推荐答案

使用AjaxDropdown从的http://真棒。codeplex.com ,你将不必再编写的JavaScript

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

这篇关于就此问题设立的MVC级联下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆