有关使用jquery $ .post将数据发布到控制器的问题

时间:2013-07-17 14:44:19

标签: asp.net-mvc

我有两个下拉列表用于国家,另一个用于州。当第一次表单呈现时,将填充国家/地区下拉列表并禁用状态下拉列表。当用户选择国家时,则通过jquery发生部分发布,并调用返回json数据的控制器操作方法。用户选择国家时出现问题,然后我看到我的重载索引方法被调用,它返回json但是firebug显示错误,如网络错误。

这里我给出了我的控制器代码和cshtml代码。请告诉我那里有什么问题

cshtml code

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

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/jscript"></script>
<script type="text/jscript">
    $(function () {
        //        $('#CountryID').change(function () {
        //            var URL = $('#FormID');
        //            alert(URL);
        ////            $.getJSON('/DDL/DistrictList/' + $('#State').val(), function (data) {
        //            });
        //        });


        $('#CountryID').change(function () {
            var dropdownID = $(this).val();
            $.post('@Url.Action("Index", "Home")', { CountryID: dropdownID }, function (result) {
                // result contains your State-List
                var items = '<option>--Select State--</option>';
                $.each(data, function (i, state) {
                    alert(i);
                    items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
                });
                $('#State').html(items);

            });
        });
    });
</script>



@using (Html.BeginForm())
{
    <fieldset>
        <legend>DropDownList</legend>
        @Html.Label("Country")
        @Html.DropDownList("Country", ViewBag.Country as SelectList, "--Select Country--", new { id = "CountryID" })

        @Html.Label("State")
        @Html.DropDownList("State", ViewBag.Country as SelectList, "--Select State--", new { id = "StateID", @disabled = "disabled" })

        <input type="submit" value="Create" id="SubmitId" />
    </fieldset>
}

controller code

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

namespace CascadeTest.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to Cascade Test";
            ViewBag.Country = new SelectList(CascadeTest.Models.Country.GetCountries(), "CountryID", "Name");

            var State = from s in CascadeTest.Models.State.GetState()
                        where s.CountryID == ""
                        select s.Name;

            ViewBag.State = new SelectList(State, "StateID", "Name");

            return View();
        }

        [HttpPost]
        public JsonResult Index(string CountryID)
        {
            var State = from s in CascadeTest.Models.State.GetState()
                        where s.CountryID == CountryID
                        select s.Name;
            string xxx = Json(new SelectList(State.ToArray(), "StateID", "Name"), JsonRequestBehavior.AllowGet).ToString();
            return Json(new SelectList(State.ToArray(), "StateID", "Name"), JsonRequestBehavior.AllowGet);
        }
    }
}

my model code

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

namespace CascadeTest.Models
{
    public class Country
    {
        public string CountryID { get; set; }
        public string Name { get; set; }

        public static IQueryable<Country> GetCountries()
        {
            return new List<Country>
            {
                new Country {
                    CountryID = "CA",
                    Name = "Canada"
                },
                new Country{
                    CountryID = "US",
                    Name = "United States"
                },
                new Country{
                    CountryID = "UK",
                    Name = "United Kingdom"
                },
                new Country{
                CountryID = "IN",
                Name = "India"
                }
            }.AsQueryable();
        }
    }

    public class State
    {
        public string StateID { get; set; }
        public string Name { get; set; }
        public string CountryID { get; set; }

        public static IQueryable<State> GetState()
        {
            return new List<State>
            {
                // State declare for india
                new State {
                    StateID = "WB",
                    Name = "West Bengal"
                    ,CountryID="IN"
                },
                new State{
                    StateID = "BH",
                    Name = "Bihar"
                    ,CountryID="IN"
                },
                new State{
                    StateID = "OR",
                    Name = "Orissa"
                    ,CountryID="IN"
                },
                new State{
                StateID = "MD",
                Name = "Madhya Pradesh"
                ,CountryID="IN"
                },

                // State declare for USA
                new State{
                StateID = "AL",
                Name = "Alabama"
                ,CountryID="US"
                },

                new State{
                StateID = "FL",
                Name = "Florida"
                ,CountryID="US"
                },

                new State{
                StateID = "IA",
                Name = "Iowa"
                ,CountryID="US"
                },

                new State{
                StateID = "MS",
                Name = "Mississippi"
                ,CountryID="US"
                },

                // State declare for UK
                new State{
                StateID = "AV",
                Name = "Avon"
                ,CountryID="UK"
                },

                new State{
                StateID = "BS",
                Name = "Buckinghamshire"
                ,CountryID="UK"
                },


                new State{
                StateID = "CH",
                Name = "Cheshire"
                ,CountryID="UK"
                },

                new State{
                StateID = "DS",
                Name = "Dorset"
                ,CountryID="UK"
                }

                //No state define for canada
            }.AsQueryable();
        }
    }
}

我认为问题出在下面的代码

$('#CountryID').change(function () {
            var dropdownID = $(this).val();
            $.post('@Url.Action("Index", "Home")', { CountryID: dropdownID }, function (result) {
                // result contains your State-List
                var items = '<option>--Select State--</option>';
                $.each(data, function (i, state) {
                    alert(i);
                    items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
                });
                $('#State').html(items);

            });
        });

这是firefox正在显示的错误行

enter image description here

我无法理解我做错了什么。所以请帮我解决这个问题。感谢

我的索引方法如何

一个是默认情况下没有参数,第二个索引方法被调用以在选择国家时填充状态。当选择country时,则通过jquery发出部分帖子,第二个索引方法从服务器端返回json,jquery解析为填充状态下拉列表。请查看代码并告诉我需要更改的位置。感谢

public ActionResult Index()
        {
            ViewBag.Message = "Welcome to Cascade Test";
            ViewBag.Country = new SelectList(CascadeTest.Models.Country.GetCountries(), "CountryID", "Name");

            var State = from s in CascadeTest.Models.State.GetState()
                        where s.CountryID == ""
                        select s.Name;

            ViewBag.State = new SelectList(State, "StateID", "Name");

            return View();
        }

        [HttpPost]
        public JsonResult Index(string CountryID)
        {
            var State = from s in CascadeTest.Models.State.GetState()
                        where s.CountryID == CountryID
                        select s.Name;
            string xxx = Json(new SelectList(State.ToArray(), "StateID", "Name"), JsonRequestBehavior.AllowGet).ToString();
            return Json(new SelectList(State.ToArray(), "StateID", "Name"), JsonRequestBehavior.AllowGet);
        }

1 个答案:

答案 0 :(得分:1)

500错误代码意味着很可能在控制器的操作方法中生成了运行时错误。如果您实际检查Firebug的Net选项卡中的响应,它应该告诉您错误是什么。

关闭袖带,例如,我可以看到您没有检查以确定是否从查询中实际返回了任何State。它可能是null,如果它为null,那么你将得到运行时错误到“null属性没有这个方法”的效果。