【问题标题】:mvc dropdow change does not triggermvc 下拉列表更改不会触发
【发布时间】:2016-11-29 08:53:09
【问题描述】:

我尝试在 mvc 页面上使用 3 个下拉列表,如果我更改 ddl 1,则 ddl 2 中的值应该会更改,而 ddl 2 的更改应该会更改 ddl 3 中的值。到目前为止,我有这个代码.. .. ddl 1 的值已设置,但如果我更改 ddl 1 中的值,则不会发生任何事情。 ddl 2 没有得到任何值,根本没有触发“public JsonResult GetPapperByTypeId(int typeId)”。

我在这里错过了什么?

主页视图

                            @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "myForm", name = "papper_search" }))
                            {

                                            @Html.DropDownList("PrintType", ViewData["papperType"] as List<SelectListItem>, new { @class = "form-control" })
                                            <br />
                                            <br />
                                            @Html.DropDownList("Papper", new SelectList(string.Empty, "Value", "Text"), "Please select a paper", new { @class = "form-control" })

                                            <br />
                                            <br />
                                            @Html.DropDownList("PapperType", new SelectList(string.Empty, "Value", "Text"), "Please select a type", new { @class = "form-control" })

                            }





<script type="text/javascript" src="@Url.Content("~/js/jquery.min.js")"></script>
<script type="text/jscript">
$(function ()
{
    $('#PrintType').change(function ()
    {
        $.getJSON('/Home/GetPapperByTypeId/' + $('#PrintType').val(), function (data)
        {
            var items = '<option>Select Papper</option>';
            $.each(data, function (i, printtype)
            {
                items += "<option value='" + printtype.Value + "'>" + printtype.Text + "</option>";
            });
            $('#Papper').html(items);
        });
    });

    $('#Papper').change(function ()
    {
        $.getJSON('/Home/Citylist/' + $('#Papper').val(), function (data)
        {
            var items = '<option>Select PapperType</option>';
            $.each(data, function (i, pappertype)
            {
                items += "<option value='" + pappertype.Value + "'>" + pappertype.Text + "</option>";
            });
            $('#PapperType').html(items);
        });
    });
});
</script>

家庭控制器

   public ActionResult Index()
    {

        var li = new List<SelectListItem>
        {
            new SelectListItem {Text = "Select", Value = "0"},
            new SelectListItem {Text = "Plain paper", Value = "1"},
            new SelectListItem {Text = "Heavy paper", Value = "2"},
        };
        ViewData["papperType"] = li;

    }

    //Action result for ajax call
    public JsonResult GetPapperByTypeId(int typeId)
    {
        var objallPappers = GetAllPappers().Where(m => m.TypeId == typeId).ToList();
        var obgpapper = new SelectList(objallPappers, "Id", "Name", 0);
        return Json(obgpapper, JsonRequestBehavior.AllowGet);
    } 


    public List<Papper> GetAllPappers()
    {
        var objPapper = new List<Papper>
        {
            new Papper {Id = 1, TypeId = 1, Name = "papper-1"},
            new Papper {Id = 2, TypeId = 2, Name = "papper-1"},
            new Papper {Id = 3, TypeId = 4, Name = "papper-1"},
            new Papper {Id = 4, TypeId = 1, Name = "papper-2"},
            new Papper {Id = 5, TypeId = 1, Name = "papper-3"},
            new Papper {Id = 6, TypeId = 4, Name = "papper-2"}
        };
        return objPapper;
    }               

【问题讨论】:

  • 您是否遇到任何错误。签入控制台窗口
  • 我现在看到我收到了这个错误...“TypeError: $ is not a function”在我的 jScript 中..
  • 这意味着你没有包含jquery或者它没有正确加载或者存在命名冲突
  • 感谢斯蒂芬提供的信息。我现在开始工作了:)

标签: asp.net-mvc cascadingdropdown


【解决方案1】:

问题:就在这行代码

$.getJSON('/Home/GetPapperByTypeId/' + $('#PrintType').val(), function (data)

实际问题是这个/Home/GetPapperByTypeId/' + $('#PrintType').val()

这最终会得到一个类似/Home/GetPapperByTypeId/SomeValue的网址

解决方案:你想要的是像/Home/GetPapperByTypeId/?typeId=SomeValue一样的URL

注意添加到查询字符串中的 ?typeId=。因为您的控制器 Action 需要一个名为 typeId 的参数

public JsonResult GetPapperByTypeId(int typeId)

因此,将 jquery 中的语法更改为

$.getJSON('/Home/GetPapperByTypeId/?typeId=' + $('#PrintType').val(), function (data)

您在这行代码中遇到了类似的问题。

$.getJSON('/Home/Citylist/' + $('#Papper').val(), function (data) 也解决这个问题..

【讨论】:

  • 由于我得到的“TypeError: $ is not a function”,我无法检查此更改是否有任何不同......
  • @MTplus 仅当jQuery 文件未正确加载到您的页面时才会出现该错误。还将您的"text/jscript" 更改为"text/javascript",因为我认为您的代码中不需要jscript
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多