C# 如何将所选下拉列表值检索到控制器?

C# 如何将所选下拉列表值检索到控制器?,c#,asp.net-mvc,model-view-controller,visual-studio-2015,C#,Asp.net Mvc,Model View Controller,Visual Studio 2015,MVC方法让我很难理解如何实际检索数据 查看 <p> @using (Html.BeginForm("About")) { <span style="margin-top: 1em; float: right;"> Filter Zone Area @Html.DropDownList("ZoneArea", new SelectListItem[] { new Sel

MVC方法让我很难理解如何实际检索数据

查看

<p>
    @using (Html.BeginForm("About"))
    {
        <span style="margin-top: 1em; float: right;">
            Filter Zone Area @Html.DropDownList("ZoneArea", new SelectListItem[]
            {
            new SelectListItem() { Text = "All", Value = "0" },
            new SelectListItem() { Text = "North", Value = "1" },
            new SelectListItem() { Text = "South", Value = "2"},
            new SelectListItem() { Text = "East", Value = "3" },
            new SelectListItem() { Text = "West", Value = "4" },
            new SelectListItem() { Text = "Central", Value = "5" },},
            new { @onchange = "this.form.submit()" })
        </span>

    }
</p>
当我调试下面的代码时,它会返回null值

string strDDLValue = form["ZoneArea"]; 

代码中没有问题。我能够通过在视图和控制器中使用完全相同的代码来获得值。区别在于@using(Html.BeginForm(“About”,“Default”)),其中默认值是控制器名称。另外,如果我们使用Request.Form[“ZoneArea”],那么我们可以在操作中去掉FormCollection参数

Stephen Muecke在注释中给出的解决方案是正确的,但这里我只是说,从代码中的视图获取控制器中的值没有错误

    public ActionResult Index()
    {
        ViewBag.SelectList = GetDict();

        return View();
    }

    public ActionResult About()
    {
        string strDDLValue = Request.Form["ZoneArea"];
        ViewBag.SelectedValue = strDDLValue;
        ViewBag.SelectList = GetDict();
        return View("Index");
    }

    public Dictionary<string,int> GetDict()
    {
        Dictionary<string, int> dict = new Dictionary<string, int>();
        dict.Add("All", 0);
        dict.Add("North", 1);
        dict.Add("South", 2);
        dict.Add("East", 3);
        dict.Add("West", 4);
        dict.Add("Central", 4);

        return dict;
    }
public ActionResult Index()
{
ViewBag.SelectList=GetDict();
返回视图();
}
关于()的公共行动结果
{
字符串strDDLValue=Request.Form[“ZoneArea”];
ViewBag.SelectedValue=strDDLValue;
ViewBag.SelectList=GetDict();
返回视图(“索引”);
}
公共词典GetDict()
{
Dictionary dict=新字典();
dict.Add(“全部”,0);
添加(“北”,1);
添加(“南”,2);
添加(“东”,3);
添加(“西部”,第4节);
添加(“中央”,第4节);
返回命令;
}
视图:

@使用(Html.BeginForm(“关于”、“默认”))
{
过滤区域区域@Html.DropDownList(“ZoneArea”,新选择列表(ViewBag.SelectList,“值”,“键”,ViewBag.SelectedValue),
新建{@onchange=“this.form.submit()”})
}

您在这里所做的一切都是不好的做法。在控制器中而不是视图中生成您的
SelectList
。将dropdownlist绑定到模型属性(是否
ZoneArea
是模型中的属性?)并发回模型-不要使用
FormCollection
谢谢,有一个问题是如何在重定向后保持选中的值?我现在的问题是重新加载页面,并重新生成下拉列表中的selectedvalue
    public ActionResult Index()
    {
        ViewBag.SelectList = GetDict();

        return View();
    }

    public ActionResult About()
    {
        string strDDLValue = Request.Form["ZoneArea"];
        ViewBag.SelectedValue = strDDLValue;
        ViewBag.SelectList = GetDict();
        return View("Index");
    }

    public Dictionary<string,int> GetDict()
    {
        Dictionary<string, int> dict = new Dictionary<string, int>();
        dict.Add("All", 0);
        dict.Add("North", 1);
        dict.Add("South", 2);
        dict.Add("East", 3);
        dict.Add("West", 4);
        dict.Add("Central", 4);

        return dict;
    }
@using (Html.BeginForm("About","Default"))
{
    <span style="margin-top: 1em; float: right;">
        Filter Zone Area @Html.DropDownList("ZoneArea", new SelectList(ViewBag.SelectList,"value","key",ViewBag.SelectedValue),
        new { @onchange = "this.form.submit()" })
    </span>

}