Jquery MVC3:JSon/Javascript——一个下拉框的内容取决于另一个下拉框的选择

Jquery MVC3:JSon/Javascript——一个下拉框的内容取决于另一个下拉框的选择,jquery,json,asp.net-mvc-3,Jquery,Json,Asp.net Mvc 3,我发现将一个示例应用程序转换为适合我需要的应用程序时遇到问题。我一直无法弄清楚我做错了什么。在转换后的应用程序中通过JSon调用返回后,我甚至没有收到警报。首先,我将复制示例的模型、控制器和视图。然后我将复制转换后应用程序的模型、控制器和视图。 我在调用控制器时遇到了一个断点,看起来SelectList数据已正确打包到JSon对象中。但我一定是做错了什么。如果您能帮助我们理解这个问题,我们将不胜感激 ---------------------------- Working Sample Mode

我发现将一个示例应用程序转换为适合我需要的应用程序时遇到问题。我一直无法弄清楚我做错了什么。在转换后的应用程序中通过JSon调用返回后,我甚至没有收到警报。首先,我将复制示例的模型、控制器和视图。然后我将复制转换后应用程序的模型、控制器和视图。 我在调用控制器时遇到了一个断点,看起来SelectList数据已正确打包到JSon对象中。但我一定是做错了什么。如果您能帮助我们理解这个问题,我们将不胜感激

---------------------------- Working Sample Model ------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SimpleMVCJSONSample.Models
{
public class MyViewModel
{
    public int? Year { get; set; }
    public int? Month { get; set; }

    public IEnumerable<SelectListItem> Years
    {
        get
        {
            return Enumerable.Range(2000, 12).Select(x => new SelectListItem
            {
                Value = x.ToString(),
                Text = x.ToString()
            });
        }
    }
}
}

-------------------------- Working Sample Controller ------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.Mvc;
using SimpleMVCJSONSample.Models;

namespace SimpleMVCJSONSample.Controllers
{

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel();
        return View(model);
    }

    public ActionResult Months(int year)
    {
        if (year == 2011)
        {
            var jsonRet1 = Json(
                Enumerable.Range(1, 3).Select(x => new { value = x, text = x }),
                JsonRequestBehavior.AllowGet
            );
            return jsonRet1;
        }
             var jsonRet2 = Json(
            Enumerable.Range(1, 12).Select(x => new { value = x, text = x }),
            JsonRequestBehavior.AllowGet
        );
        return jsonRet2;
    }
}
}

-------------------------- Working Sample View -----------------

@{
ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>

@model SimpleMVCJSONSample.Models.MyViewModel

@Html.DropDownListFor(
x => x.Year, 
new SelectList(Model.Years, "Value", "Text"),
"-- select year --"
)

@Html.DropDownListFor(
x => x.Month, 
Enumerable.Empty<SelectListItem>(),
"-- select month --"
)

<script type="text/javascript">
$('#Year').change(function () {
    var selectedYear = $(this).val();
    if (selectedYear != null && selectedYear != '') {
        $.getJSON('@Url.Action("Months")', { year: selectedYear }, function (months) {
            alert(months);
            var monthsSelect = $('#Month');
            monthsSelect.empty();
            $.each(months, function (index, month) {
                monthsSelect.append($('<option/>', {
                    value: month.value,
                    text: month.text
                }));
            });
        });
    }
});
</script>

---------------------------------END OF Working SAMPLE-----------------------

--------------------------My Converted Model ------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using ONSM.Common.Logging;
using VW40.Data.Model;
using VW40.Services;

namespace VW40.Web.Models
{
public class CompanyPeopleViewModel
{
    private Log _log;
    private ExceptionManager _exceptionManager;
    private readonly ICompanyService _companyService;
    private readonly IPersonService _personService;

    public CompanyPeopleViewModel(Log log, ExceptionManager exceptionManager,     ICompanyService companyService, IPersonService personService)
    {

        _companyService = companyService;
        _personService = personService;
        _log = log;
        _exceptionManager = exceptionManager;
    } 

    public int Company { get; set; }
    public int Person { get; set; }

    public IEnumerable<SelectListItem> CompanySelectList
    {
        get
        {
            return _companyService.GetCompanies().OrderBy(x => x.CompanyName).Select(x => new SelectListItem
                    {
                        Value = x.CompanyID.ToString(),
                        Text = x.CompanyName

                    });
        }
    }
}
}

--------------------------- My Converted Controller ----------------

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using ONSM.Common.Logging;
using VW40.Data.Model;
using VW40.MVC.Models;
using VW40.Services;
using VW40.Services.AccessControl;
using VW40.Web.Models;
using System.Web;


namespace VW40.MVC.Controllers
{
[Authorize(Roles = "Administrator")]
public class AccessControlController : MembershipAdministrationController_Base
{
    private Log _log; 
    private ExceptionManager _exceptionManager;
    private IAccessControlService _accessControlService;
    private IPersonService _personControlService;
    private ICompanyService _companyService;

    public AccessControlController(
        Log log,
        ExceptionManager ExceptionManager,
        IAccessControlService AccessControlService,
        IPersonService PersonService,
        ICompanyService CompanyService
        )
    {
        _log = log;
        _exceptionManager = ExceptionManager;
        _accessControlService = AccessControlService;
        _personControlService = PersonService;
        _companyService = CompanyService;
    }

    public ActionResult Json_CompanyPeopleSelectList(int CompanyID)
    {


        var companyPersonsList = _personControlService.GetPersonsByCompanyId(CompanyID).OrderBy(x => x.Lastname + x.Firstname);

        //IEnumerable<SelectListItem> myList = companyPersonsList.Select(x => new SelectListItem
        //                                                                        {
        //                                                                                Value = x.PersonID.ToString(),
        //                                                                            Text = x.Lastname
        //                                                                        }
            //);


        var jsonRet =
            Json(
                _personControlService.GetPersonsByCompanyId(CompanyID).OrderBy(x => x.Lastname + x.Firstname).Select
                    (x => new
                              {
                                  value = x.PersonID.ToString(),
                                  text = x.Lastname
                              }), JsonRequestBehavior.AllowGet);
        return jsonRet;
    }
-----------------------------工作示例模型------------
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
命名空间SimpleMVCJSONSample.Models
{
公共类MyViewModel
{
公共整数?年份{get;set;}
公共整数?月{get;set;}
公历数年
{
收到
{
返回可枚举的.Range(2000,12).Select(x=>newselectListItem
{
值=x.ToString(),
Text=x.ToString()
});
}
}
}
}
--------------------------工作样本控制器------------------
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用SimpleMVCJSONSample.Models;
命名空间SimpleMVCJSONSample.Controllers
{
公共类HomeController:控制器
{
公共行动结果索引()
{
var模型=新的MyViewModel();
返回视图(模型);
}
公共行动结果月数(整年)
{
如果(年份==2011年)
{
var jsonRet1=Json(
范围(1,3)。选择(x=>new{value=x,text=x}),
JsonRequestBehavior.AllowGet
);
返回jsonRet1;
}
var jsonRet2=Json(
范围(1,12)。选择(x=>new{value=x,text=x}),
JsonRequestBehavior.AllowGet
);
返回jsonRet2;
}
}
}
--------------------------工作示例视图-----------------
@{
ViewBag.Title=“主页”;
}
@查看包。留言
@模型SimpleMVCJSONSample.Models.MyViewModel
@Html.DropDownListFor(
x=>x.年,
新的选择列表(Model.Years、“Value”、“Text”),
“--选择年份--”
)
@Html.DropDownListFor(
x=>x.月,
Enumerable.Empty(),
“--选择月份--”
)
$(“#年”)。更改(函数(){
var selectedYear=$(this.val();
如果(selectedYear!=null&&selectedYear!=''){
$.getJSON('@Url.Action(“月”),{year:selectedYear},函数(月){
警报(月);
var monthselect=$('月');
monthselect.empty();
$。每个(月,功能(索引,月){
月选择。追加($(''){
value:month.value,
text:month.text
}));
});
});
}
});
---------------------------------工作样本结束-----------------------
--------------------------我的转换模型------------------------
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
使用ONSM.Common.Logging;
使用VW40.Data.Model;
使用VW40.服务;
命名空间VW40.Web.Models
{
公共类companyopelvewmodel
{
私人日志(u Log),;
私人例外管理者(u例外管理者),;
私人只读ICompanyService(公司服务);
私有只读IPersonService\u personService;
公共公司员工预览模型(日志日志、例外管理器例外管理器、ICompanyService companyService、IPersonService personService)
{
_公司服务=公司服务;
_personService=personService;
_log=log;
_例外管理器=例外管理器;
} 
公共整数公司{get;set;}
公共整数Person{get;set;}
公共IEnumerable公司选择列表
{
收到
{
return _companyService.getcompanys().OrderBy(x=>x.CompanyName)。选择(x=>newselectListItem
{
Value=x.CompanyID.ToString(),
Text=x.CompanyName
});
}
}
}
}
---------------------------我的转换控制器----------------
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web.Mvc;
使用Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
使用ONSM.Common.Logging;
使用VW40.Data.Model;
使用VW40.MVC.Models;
使用VW40.服务;
使用VW40.Services.AccessControl;
使用VW40.Web.Models;
使用System.Web;
命名空间VW40.MVC.Controllers
{
[授权(角色=“管理员”)]
公共类AccessControlController:MembershipAdministrationController\u Base
{
私人日志(u Log),;
私人例外管理者(u例外管理者),;
专用IAccessControlService\u访问控制服务;
私人IPersonService(个人控制服务);;
私人公司服务;
公共访问控制器(
日志,
例外管理器例外管理器,
IAccessControlService访问控制服务,
IPersonService个人服务,
I公司服务公司服务
)
{
_log=log;
_例外管理器=例外管理器;
_accessControlService=accessControlService;
_PersonControl服务=PersonService;
_公司服务=公司服务;
}
public ActionResult Json_CompanyOpleSelectList(int CompanyID)
{
var companyPersonsList=\u personControlService.GetPersonsByCompanyId(CompanyID).OrderBy(x=>x.Lastname+x.Firstname);
//IEnumerable myList=CompanyPersonList.Select(x=>new SelectListItem
//
@model VW40.Web.Models.CompanyPeopleViewModel
@{
Layout = "~/Views/Shared/SiteLayout.cshtml";
ViewBag.Title = "UserPermissions";
}

<h2>UserPermissions</h2>

@Html.DropDownListFor(
x => x.Company, 
new SelectList(Model.CompanySelectList, "Value", "Text"),
"Select Company"
)

@Html.DropDownListFor(
    x => x.Person,
    Enumerable.Empty<SelectListItem>(),
    "-- Select Person --"
)
<script type="text/javascript">
$('#Company').change(function () {
    var selectedCompany = $(this).val();
    if (selectedCompany != null && selectedCompany != '') {
        $.getJSON('@Url.Action("Json_CompanyPeopleSelectList")', { CompanyID:     selectedCompany }, function (listOfCompanyPeople) {
            alert(listOfCompanyPeople);
            var personSelect = $('#Person');
            personSelect.empty();
            $.each(listOfCompanyPeople, function (index, person) {
                personSelect.append($('<option/>', {
                    value: person.value,
                    text: person.text
                }));
            });
        });
    }
});
</script>

------------------------------ end of Conversion ----------------