如何为特定用户填充下拉列表?

时间:2015-06-02 06:25:09

标签: asp.net-mvc

我有三个下拉列表:

 @Html.DropDownListFor(m => m.Edit.SelectCountryId, Model.Edit.Countries, "Please select", new { id = "CountryID", @class = "form-control", @onchange = "LoadRegions();" })
 @Html.DropDownListFor(m => m.Edit.SelectRegionId,Model.Edit.Region, "Please select", new { id = "RegionID", @class = "form-control", @onchange = "LoadCities();" })
 @Html.DropDownListFor(m => m.Edit.SelectCityId, Model.Edit.City, "Please select", new { id = "CityID", @class = "form-control" })

我根据第一个和第三个填充第二个,具体取决于第二个...但是当我想显示某些特定用户的下拉列表时我只能显示国家

我的模特:

 public int SelectCountryId { get; set; }
 public int SelectRegionId { get; set; }
 public int SelectCityId {get; set;}
 public System.Web.Mvc.SelectList City { get; set; }
 public System.Web.Mvc.SelectList Region{ get; set; }
 public System.Web.Mvc.SelectList Country{ get; set; }

我的控制器:

model.Edit.SelectCountryId = user.ContactInformation.First().City.Region.Country.Id;
model.Edit.SelectRegionId = user.ContactInformation.First().City.Region.Id;
model.Edit.SelectCityId = user.ContactInformation.First().City.Id;

我在控制器上获取值但我无法在视图中显示它们

编辑: 我的脚本:

 var region = $('#RegionID');


    var urlCountries = "/Account/GetRegions";
    $.getJSON(urlCountries, { countryId: $('#CountryID').val() }, function (response) {
        // clear and add default (null) option

        region.empty().append($('<option></option>').val('').text('Please select'));


        for (var i = 0; i < response.length; i++) {
            region.append($('<option></option>').val(response[i].Id).text(response[i].Name));
        }

    });

    var city = $('#CityID');
    var url = "/Account/GetCities"; // use the helper (dont hard code)
    $.getJSON(url, { regionId: $('#RegionID').val() }, function (response) {
        // clear and add default (null) option

        city.empty().append($('<option></option>').val('').text('Please select'));

        for (var i = 0; i < response.length; i++) {
            city.append($('<option></option>').val(response[i].Id).text(response[i].Name));
        }

    });

我的GetCity()和GetRegions()

方法
  public IEnumerable<IRegion> GetRegions(int countryId)
        {
            Model.Administration.Region[] modelRegions = Model.Administration.Region.FindAll(DetachedCriteria.For<Model.Administration.Region>().Add(Restrictions.Eq("Country.Id", countryId)));

            return modelRegions.Select(Region.ConvertFromModel).Cast<IRegion>().ToList();
        }

  public IEnumerable<ICity> GetCities(int regionId)
        {
            CityLimited[] modelCities = CityLimited.FindAll(DetachedCriteria.For<CityLimited>().Add(Restrictions.Eq("Region.Id", regionId)));

            return modelCities.Select(City.ConvertFromModel).Cast<ICity>().ToList();
        }

1 个答案:

答案 0 :(得分:0)

您需要传递一个额外的值,指示您返回视图的json数据中的所选选项。您的GetRegions()方法看起来应该是

public JsonResult GetRegions(int countryId) // return a JsonResult
{
    Model.Administration.Region[] modelRegions = Model.Administration.Region.FindAll(DetachedCriteria.For<Model.Administration.Region>().Add(Restrictions.Eq("Country.Id", countryId)));
    var options =  modelRegions.Select(Region.ConvertFromModel).Cast<IRegion>().ToList();
    var selection = 1; // set the value of the selected option here - must match the `ID` property of one of one of the `IRegion` your returning
    return Json(new { options = options, selection = selection }, JsonRequestBehavior.AllowGet);
}

附注:不清楚modelRegions.Select(Region.ConvertFromModel).Cast<IRegion>().ToList();返回的内容,但如果IRegion包含的只是视图所需的IDName属性,请考虑创建一个集合匿名对象,因此您不会通过网络发送不必要的数据

然后相应的脚本应该是

var url = '@Url.Action("GetRegions", "Account")'; // use the helper (dont hard code)
var regions = $('#RegionID'); // cache the element
$('#CountryID').change(function() {
    $.getJSON(url, { id: $(this).val() }, function(response) {
        // clear and add default (null) option
        regions.empty().append($('<option></option>').val('').text('Please select'));
        $.each(response.options, function(index, item) {
            cities.append($('<option></option>').val(item.Value).text(item.Text));
        });
        regions.val(response.selection); // set the selected value
    });
});

并从标记中删除@onchange = "LoadRegions();

同样适用于GetCities()控制器方法和相应的脚本