Asp.net mvc 包含填充where子句的下拉列表

Asp.net mvc 包含填充where子句的下拉列表,asp.net-mvc,Asp.net Mvc,我想用数据库中的值填充第二个下拉列表GetCity(),这些值取决于第一个列表或GetCounty()下拉列表中选择的内容。在哪里添加Where子句 public class NewsModel : BaseModel { [Required] public int? FKCountyId { get; set; } public string County { get; set; } [Required] public int? FKCityId { g

我想用数据库中的值填充第二个下拉列表
GetCity()
,这些值取决于第一个列表或
GetCounty()
下拉列表中选择的内容。在哪里添加Where子句

public class NewsModel : BaseModel
{
    [Required]
    public int? FKCountyId { get; set; }
    public string County { get; set; }
    [Required]
    public int? FKCityId { get; set; }
    public string City { get; set; }


    public List<SelectListItem> GetCounty()
    {
        List<SelectListItem> lst = new List<SelectListItem>();
        lst.Add(new SelectListItem() { Text = "Please select County", Value = "" });
        foreach (var item in LambertonContext.NewsCounties)
        {
            lst.Add(new SelectListItem() { Text = item.County, Value = item.PKCountyId.ToString() });
        }
        return lst;
    }

    public List<SelectListItem> GetCity()
    {
        List<SelectListItem> lst = new List<SelectListItem>();
        lst.Add(new SelectListItem() { Text = "Please select City", Value = "" });
        foreach (var item in LambertonContext.NewsCities)
        {
            lst.Add(new SelectListItem() { Text = item.City, Value = item.PKCityId.ToString() });
        }
        return lst;
        }
}


 <div class="panel-body">
                <div class="form-group">
                    @Html.LabelFor(m => m.FKCountyId, new { @class = "col-sm-2 col-sm-2 control-label" })
                    <div class="col-sm-10">
                        @Html.DropDownListFor(m => m.FKCountyId, Model.GetCounty())
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.FKCityId, new { @class = "col-sm-2 col-sm-2 control-label" })
                    <div class="col-sm-10">
                        @Html.DropDownListFor(m => m.FKCityId, Model.GetCity())
                    </div>
                </div>
公共类新闻模型:BaseModel
{
[必需]
公共int?FKCountyId{get;set;}
公共字符串country{get;set;}
[必需]
公共int?FKCityId{get;set;}
公共字符串City{get;set;}
公共列表GetCounty()
{
List lst=新列表();
lst.Add(新建SelectListItem(){Text=“请选择县”,Value=”“});
foreach(LambertonContext.newsCountries中的变量项)
{
添加(新SelectListItem(){Text=item.County,Value=item.PKCountyId.ToString()});
}
返回lst;
}
公共列表GetCity()
{
List lst=新列表();
lst.Add(新建SelectListItem(){Text=“请选择城市”,Value=”“});
foreach(LambertonContext.NewsCities中的var项)
{
添加(新SelectListItem(){Text=item.City,Value=item.PKCityId.ToString()});
}
返回lst;
}
}
@LabelFor(m=>m.FKCountyId,新的{@class=“col-sm-2 col-sm-2控件标签”})
@Html.DropDownListFor(m=>m.FKCountyId,Model.GetCounty())
@LabelFor(m=>m.FKCityId,新的{@class=“col-sm-2 col-sm-2控件标签”})
@Html.DropDownListFor(m=>m.FKCityId,Model.GetCity())

您需要将
CountyId
传递给您的
GetCity()
并过滤您的列表; 试试这个:

 public List<SelectListItem> GetCity(string CountyId)
    {
        List<SelectListItem> lst = new List<SelectListItem>();
        lst.Add(new SelectListItem() { Text = "Please select City", Value = "" });
        foreach (var item in LambertonContext.NewsCities.Where(FKCountyId == GetCounty().Value))
        {
            lst.Add(new SelectListItem() { Text = item.City, Value = item.PKCityId.ToString() });
        }
        return lst;
        }
公共列表GetCity(字符串CountyId)
{
List lst=新列表();
lst.Add(新建SelectListItem(){Text=“请选择城市”,Value=”“});
foreach(LambertonContext.newscienties.Where(FKCountyId==GetCounty().Value)中的var项)
{
添加(新SelectListItem(){Text=item.City,Value=item.PKCityId.ToString()});
}
返回lst;
}

Alundra,我必须回发GetCounty下拉列表才能获得下拉列表中选择的值吗?您不需要回发,您已经有了FK关系。您需要一个AJAX调用来执行填充城市下拉列表linstOK但LambertonContext.newscienties.Where(FKCountyId==GetCounty().Value)出现错误157'System.Collections.Generic.List'不包含'Value'的定义,并且找不到接受'System.Collections.Generic.List'类型的第一个参数的扩展方法'Value'(是否缺少using指令或程序集引用?)确定由第一个下拉菜单触发的ajax调用,并将在第一个下拉菜单中选择的值发送给填充第二个下拉菜单的代码。您需要对视图代码进行额外的工作,您将需要一些JS脚本代码才能使其工作。这超出了你最初问题的范围