如何在MVC 4 Razor中提交后获取所选DropDownList值的Id

时间:2013-02-28 10:41:19

标签: asp.net-mvc-4

    'public string Country;
    public List<SelectListItem> DdlCountryList { get; set; }
    public string State;
    public List<SelectListItem> DdlStateList { get; set; }
    private List<SelectListItem> _listItems;
    public string District = "District";
    public List<SelectListItem> DdlDistrictList { get; set; }
    public string Village = "Village";
    public IEnumerable<SelectListItem> DdlVillageList { get; set; }
    public string Crop = "Crop";
    public IEnumerable<SelectListItem> DdlCropList { get; set; }
    public string Year = "Year";
    public IEnumerable<SelectListItem> DdlYearList { get; set; }
    public string ProductionCode = "ProductionCode";
    public IEnumerable<SelectListItem> DdlProductionList { get; set; }
    public string Season = "Season";
    public IEnumerable<SelectListItem> DdlSeasonList { get; set; }'

> Tha Above is My Model GlobalSearchModel
> on load Iam filling Values,.. and By using Jquery change function i am calling Values how    to get the Id's of Selected DropDownValues

public ActionResult Index()
    {

        GlobalSearchModel objGlobalSearchModel = new GlobalSearchModel();
        return View(objGlobalSearchModel);
    }
  

这是My Controller的Action方法,我在GlobalSearchModel()的构造函数中填充DropDownList的默认值;比如'选择'

[HttpPost]
    public ActionResult FilterMapDetails(GlobalSearchModel objGlobalSearchModel)
    {
        //Logic GoesHere then i will return values

        return View("Index", objGlobalSearchModel);

    }
  

以上是我提交的PostMethod,提前致谢

1 个答案:

答案 0 :(得分:3)

在视图模型中,您应该为每个下拉列表提供相应的属性,这些属性将绑定到DropDownList并保留选定的值。

例如:

public string SelectedCountry { get; set; }
public List<SelectListItem> DdlCountryList { get; set; }

然后在你的视图中:

@Html.DropDownListFor(x => x.SelectedCountry, Model.DdlCountryList)

最后在您的帖子操作中,只需使用模型中的SelectedCountry属性:

[HttpPost]
public ActionResult FilterMapDetails(GlobalSearchModel objGlobalSearchModel)
{
    // objGlobalSearchModel.SelectedCountry will contain the selected value

    return View("Index", objGlobalSearchModel);
}