【问题标题】:Why is the DropDown values Null in the POST action?为什么 POST 操作中的 DropDown 值为 Null?
【发布时间】:2013-01-16 19:31:19
【问题描述】:

下面是视图:

<%@ 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>

下面提到的是型号:

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; }
    }
}

我在视图中传递了一些下拉值。 下面是控制器动作:

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);
}

为什么 POST 操作中的 DropDown 值为 Null?

【问题讨论】:

    标签: asp.net-mvc http-post html.dropdownlistfor


    【解决方案1】:

    DropDownItemsnull,因为您的表单中没有 POSTed。你只是用这个创建一个下拉菜单:Html.DropDownListFor(x =&gt; x.SelectedFavColor, Model.DropDownItems),这不足以发布收藏。

    然而,发布一个集合是相当棘手的;但你可以查看this

    最后一件事:您已经拥有该集合(因为您将其传递给View),所以从技术上讲,您甚至不需要将POST 传回服务器。

    【讨论】:

      【解决方案2】:

      DropDown 的值为 null 的简单原因是因为它们没有与表单一起发送(POST)(您可以使用 Firebug Firefox 扩展轻松检查它)。您将需要再次收集(重新填充)下拉列表的值以查看这些值。

      建议:

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

      【讨论】:

        猜你喜欢
        • 2014-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-04
        • 1970-01-01
        • 1970-01-01
        • 2019-09-11
        • 1970-01-01
        相关资源
        最近更新 更多