MVC下拉列表是有条件的

时间:2014-05-14 14:47:29

标签: asp.net-mvc

由于某种原因对我来说不适用于简单的应用程序。有人可以检查我做错了什么吗?国家和地位很好,但动物名单总是空的。

查看

@{
    ViewBag.Title = "Classic Cascading DDL";
}

@using (Html.BeginForm("IndexDDL", "Home", FormMethod.Post, 
    new { id = "CountryStateFormID",
          data_stateListAction = @Url.Action("StateList"),
          data_petListAction = @Url.Action("PetList")
    }))
{
    <fieldset>
        <legend>Country/State</legend>
        @Html.DropDownList("Countries", ViewBag.Country as SelectList,
            "Select a Country", new { id = "CountriesID" })
        <div id="StatesDivID" >
            <label for="States">States</label>
            <select id="StatesID"  name="States"></select>
        </div>
        <div id="PetsDivID">
            <label for="Pets">Pets</label>
            <select id="PetsID"  name="Pets"></select>
        </div>

        <p>
            <input type="submit" value="Submit" id="SubmitID" />
        </p>
    </fieldset>
}

<script src="@Url.Content("~/Scripts/countryState.js")"></script>

型号:国家

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

namespace CascadingDDL.Models {
    public class Country {
        public string Code { get; set; }
        public string Name { get; set; }

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

模型:状态

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

namespace CascadingDDL.Models {
    public class State {
        public string Code { get; set; }
        public string CodePet { get; set; }
        public int StateID { get; set; }
        public string StateName { get; set; }
        static int cnt = 0;  

        public static IQueryable<State> GetStates() {
            cnt = 0;
            return new List<State>
            {
                 new State
                    {
                        Code = "CA",
                        CodePet ="CAT",
                        StateID=cnt++,
                        StateName = "Nunavut"
                    },
                new State
                    {
                        Code = "CA",
                        CodePet ="DOG",
                        StateID=cnt++,
                        StateName = "Ontario"
                    },
                new State
                    {
                        Code = "US",
                        CodePet ="DOG",
                        StateID=cnt++,
                        StateName = "Washington"
                    },
                new State
                    {
                        Code = "US",
                        CodePet ="DOG",
                        StateID=cnt++,
                        StateName = "Vermont"
                    },
                    new State
                    {
                        Code = "UK",
                        CodePet ="DOG",
                        StateID=cnt++,
                        StateName = "Britian"
                    },
                     new State
                    {
                        Code = "UK",
                        CodePet ="DOG",
                        StateID=cnt++,
                        StateName = "Northern Ireland"
                    },
                     new State
                    {
                        Code = "UK",
                        CodePet ="DUCK",
                        StateID=cnt++,
                        StateName = "Scotland"
                    },
                         new State
                    {
                        Code = "UK",
                        CodePet ="DUCK",
                        StateID=cnt++,
                        StateName = "Wales"
                    }
            }.AsQueryable();
        }
    }
}

型号:宠物

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

namespace CascadingDDL.Models {
    public class Pet {
        public string CodePet { get; set; }
        public int PetID { get; set; }
        public string PetName { get; set; }
        static int cnt = 0; 

        public static IQueryable<Pet> GetPets() {
            cnt = 0;
            return new List<Pet>
            {
                 new Pet
                    {
                        CodePet = "CAT",
                        PetID=0, 
                        PetName = "Cat-1"
                    },
                new Pet
                    {
                        CodePet = "DOG",
                        PetID=1,
                        PetName = "Dog-1"
                    },
                new Pet
                    {
                        CodePet = "DUCK",
                        PetID=2,
                        PetName = "Duck-1"
                    }
            }.AsQueryable();
        }
    }
}

控制器的一部分

public ActionResult StateList(string ID) {
    string Code = ID;
    var states = from s in State.GetStates()
                 where s.Code == Code
                 select s;

    if (HttpContext.Request.IsAjaxRequest())
        return Json(new SelectList(
                        states.ToArray(),
                        "StateID",
                        "StateName")
                   , JsonRequestBehavior.AllowGet);

    return RedirectToAction("Index");
}

public ActionResult PetList(string ID)
{
    string CodePet = ID;
    var pets = from s in Pet.GetPets()
                 where s.CodePet == CodePet
                 select s;

    if (HttpContext.Request.IsAjaxRequest())
        return Json(new SelectList(
                        pets.ToArray(),
                        "PetID",
                        "PetName")
                   , JsonRequestBehavior.AllowGet);

    return RedirectToAction("Index");
}

脚本

$(function () {

    $('#StatesDivID').hide();
    $('#PetsDivID').hide();
    $('#SubmitID').hide();

    $('#CountriesID').change(function () {
        var URL = $('#CountryStateFormID').data('stateListAction');
        $.getJSON(URL + '/' + $('#CountriesID').val(), function (data) {
            var items = '<option>Select a State</option>';
            $.each(data, function (i, state) {
                items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
            });
            $('#StatesID').html(items);
            $('#StatesDivID').show();
        });
    });

    $('#StatesID').change(function () {
        var URL = $('#CountryStateFormID').data('petListAction');
        $.getJSON(URL + '/' + $('#StatesID').val(), function (data) {
            var items = '<option>Select a Pet</option>';
            $.each(data, function (i, pet) {
                items += "<option value='" + pet.Value + "'>" + pet.Text + "</option>";
            });
            $('#PetsID').html(items);
            $('#PetsDivID').show();
        });
    });
});

无论我选择什么状态总是“选择宠物”

1 个答案:

答案 0 :(得分:1)

如果您想要检索数据,则需要在StateList Action中更改此行:

 if (HttpContext.Request.IsAjaxRequest())
     return Json(new SelectList(
                    states.ToArray(),
                    "StateID",
                    "StateName")
               , JsonRequestBehavior.AllowGet);

要:

  if (HttpContext.Request.IsAjaxRequest())
    return Json(new SelectList(
                    states.ToArray(),
                    "CodePet",
                    "StateName")
               , JsonRequestBehavior.AllowGet);

PetList Action一样,您需要使用CodePet过滤器而不是StateId。 我希望它会有所帮助