0

我正在尝试填写从下拉列表中选择国家/地区的状态 ddl。我能够根据stateList函数中的国家/地区名称标准获得过滤结果。但是当它返回视图时,它会显示所有状态列表。你能告诉我哪里出错了。

提前致谢。

//This is my controller class 
public ActionResult  Country()
{
    Country country = new Country();
    ListCountry objCountry = new ListCountry(country);
    return View(country);
}

//This method calls  on selection of countryanem   
public ActionResult stateList()
{
    Country country = new Country();
    var state = Request.Form["_countryId"];
    country.CountryId = _countryId; 
    ListCountry objCountry = new ListCountry(country);
    return View(country);
}
}

//This  is model class  
public partial class Country
{
    public SelectList countryList { get; set; }
    public SelectList stateList { get; set; }
}

public partial class ListCountry
{
    Entities db = new Entities();

    //This constructorof Listcounty  populate the country and state ddl 
    public ListCountry(Country country) 
    {
        string cntId1    = country.countryId;
        int id = Convert.ToInt32(cntId1);
        country.countryList = new SelectList(db.Countries.ToList(), "CountryId", "CountryName");
        if (id > 0 )
        {
            List <State> list = db.States.Where(p => p.countryId == id).ToList();
            country.stateList = new SelectList(list, "StateID", "StateName");
        }
        else
        {
            country.stateList = new SelectList(db.States.ToList(), "StateID", "StateName");
        }
    }
4

1 回答 1

0

几件事:

  1. 从您的描述来看,您似乎stateList在首次加载时正在调用。您是否在通话中包括国家/地区?如果您不这样做,它将始终为您提供country.countryId查询中需要的所有状态列表。

  2. 我不建议SelectList在您的模型中声明。MVC 应该将这两个问题分开。如果你突然决定不使用选择会发生什么?这应该只涉及View.

于 2013-09-20T15:02:22.333 回答