Asp.net mvc 为什么POST操作中的下拉值为空?

Asp.net mvc 为什么POST操作中的下拉值为空?,asp.net-mvc,http-post,html.dropdownlistfor,Asp.net Mvc,Http Post,Html.dropdownlistfor,以下是观点: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.Index>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent"

以下是观点:

<%@ Page Title="" Language="C#" 
         MasterPageFile="~/Views/Shared/Site.Master" 
         Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.Index>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Contact
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% using (Html.BeginForm())
    {%>
         <%=Html.DropDownListFor(x => x.SelectedFavColor, Model.DropDownItems)%>
         <%= Html.ValidationMessageFor(x=> x.SelectedFavColor) %>
         <input type="submit" value="submit" />
    <%} %>
</asp:Content>

接触
x、 已选择FAVColor,Model.DropDownItems)%%>
x、 选择的颜色)%%>
以下是模型:

namespace MvcApplication1.Models
{
    public class Index
    {
        [Range(0, 1000, ErrorMessage = "hello")]
        public int SelectedFavColor { get; set; }

        public IEnumerable<SelectListItem> DropDownItems { get; set; }
    }

    public class Colors
    {
        public int ColorID { get; set; }
        public string ColorName { get; set; }
    }
}
namespace mvcapapplication1.Models
{
公共类索引
{
[范围(0,1000,ErrorMessage=“hello”)]
public int SelectedFavColor{get;set;}
公共IEnumerable下拉项{get;set;}
}
公共类颜色
{
公共int ColorID{get;set;}
公共字符串ColorName{get;set;}
}
}
我正在视图中传递一些下拉值。 以下是控制器的操作:

public ActionResult Contact()
{
    List<MvcApplication1.Models.Colors> l = new List<Models.Colors>();
    l.Add(new Models.Colors { ColorName = "-1", ColorID = -1 });
    l.Add(new Models.Colors { ColorName = "a", ColorID = 0 });
    l.Add(new Models.Colors { ColorName = "b", ColorID = 2 });
    l.Add(new Models.Colors { ColorName = "c", ColorID = 3 });
    l.Add(new Models.Colors { ColorName = "d", ColorID = 4 });
    l.Add(new Models.Colors { ColorName = "e", ColorID = 4 });
    l.Add(new Models.Colors { ColorName = "f", ColorID = 4 });

    var model = new MvcApplication1.Models.Index
    {
        DropDownItems = l.Select(i => new SelectListItem 
                { 
                      Text = i.ColorName, 
                      Value = i.ColorID.ToString() 
                })
    };
    ViewData["records"] = model.DropDownItems;
    return View(model);
}

[HttpPost]
public ActionResult Contact(Index posted, FormCollection collection)
{
    posted.SelectedFavColor = Convert.ToInt16(collection["SelectedFavColor"]);
    return View(posted);
}
public ActionResult联系人()
{
列表l=新列表();
l、 添加(新模型.Colors{ColorName=“-1”,ColorID=-1});
l、 添加(新的Models.Colors{ColorName=“a”,ColorID=0});
l、 添加(新的Models.Colors{ColorName=“b”,ColorID=2});
l、 添加(新模型.Colors{ColorName=“c”,ColorID=3});
l、 添加(新模型.Colors{ColorName=“d”,ColorID=4});
l、 添加(新模型.Colors{ColorName=“e”,ColorID=4});
l、 添加(新模型.Colors{ColorName=“f”,ColorID=4});
var模型=新MVCAPApplication1.Models.Index
{
DropDownItems=l.Select(i=>new SelectListItem
{ 
Text=i.ColorName,
Value=i.ColorID.ToString()
})
};
ViewData[“记录”]=model.DropDownItems;
返回视图(模型);
}
[HttpPost]
公共行动结果联系人(已发布索引,FormCollection集合)
{
posted.SelectedFavColor=Convert.ToInt16(集合[“SelectedFavColor]”);
返回视图(已发布);
}

为什么POST操作中的下拉列表值为空?

由于表单中未对其进行
POST
编辑,因此
DropDownItems
Null
。您仅使用以下内容创建下拉菜单:
Html.DropDownListFor(x=>x.SelectedFavColor,Model.dropdownlitems)
,这不足以发布集合

然而,发布收藏是相当棘手的;但你可以调查一下


最后一件事:您已经拥有了该集合(因为您正在将其传递到
视图中),因此从技术上讲,您甚至不需要
将其发回服务器。

下拉列表值为null的简单原因是,它们没有随表单一起发送(发布)(您可以使用Firebug Firefox扩展轻松地进行检查)。您需要再次收集(重新填充)下拉列表中的值,才能在视图中查看这些值

建议:

请勿在发布后立即返回视图。ASP-MVC应用程序中的典型模式是POST Redirect Get。它将帮助您避免不必要地重新发布表单(例如,在浏览器刷新按钮上)-