c# - 在 MVC 中使用下拉菜单时出现错误?

标签 c# asp.net-mvc-4

我在这里使用下拉列表并收到类似

的错误
ArgumentNullException was unhandled by user code 
Value cannot be null.
Parameter name: items

我收到这个错误是因为在发布期间我得到了项目的空值。 我试过这个样本 Dropdown in MVC 这是我的下拉列表

@Html.DropDownListFor(m => m.SelectedItem, new SelectList(Model.Items, "Value", "Text")})

和我的模型

 public class OptimizeModels
    {   
 public string SelectedItem { get; set; }
 public IEnumerable<Item> Items { get; set; }
}
 public class Item
    {
        public string Value { get; set; }
        public string Text { get; set; }
    }

和我的 Controller

public ActionResult Optimize()
        {
            var model = new OptimizeModels
            {                
                Items = new[] 
                {
                    new Item { Value = "Sales", Text = "Units" },
                    new Item { Value = "RetGM", Text = "Rtlr Gross Margin ($)" },
                    new Item { Value = "MfrGM", Text = "Mfr Gross Margin ($)" },
                }
            };
            return View(model);
        }
[HttpPost]
   public ActionResult Optimize(OptimizeModels model)
        {
            ObjOptimizeService = new OptimizeEventPerformance();

            if (ModelState.IsValid)
            {
                ObjOptimizeInputParameter.ObjectivetoOptimize = model.SelectedItem;
                model.ResponseXML = resultXMLContent;
                XmlDocument xdoc = new XmlDocument();
                xdoc.LoadXml(resultXMLContent);
                xdoc.Save(Server.MapPath("..\\XML_Files\\OutputXML.xml"));
            }
            model.ChartName = ObjCommon.GetFusionSWFReportName("Optimization", "OEP_3");
            //return PartialView("../Home/RenderFusionChartView", model);
            return View(model);
        }

有什么建议吗?

最佳答案

在您的 HttpPost 操作中,您忘记在呈现 View 之前重新绑定(bind) DropDown 的值。由于集合永远不会发布到服务器,因此您需要像在 GET 操作中那样填充它:

[HttpPost]
public ActionResult Optimize(OptimizeModels model)
{
    ObjOptimizeService = new OptimizeEventPerformance();
    if (ModelState.IsValid)
    {
        ObjOptimizeInputParameter.ObjectivetoOptimize = model.SelectedItem;
        model.ResponseXML = resultXMLContent;
        XmlDocument xdoc = new XmlDocument();
        xdoc.LoadXml(resultXMLContent);
        xdoc.Save(Server.MapPath("..\\XML_Files\\OutputXML.xml"));
    }
    model.ChartName = ObjCommon.GetFusionSWFReportName("Optimization", "OEP_3");

    // if you intend to redisplay the same view you need to assign a value
    // for the Items property because your view relies on it (you have bound
    // a dropdownlist to it, remember?)
    model.Items = new[] 
    {
        new Item { Value = "Sales", Text = "Units" },
        new Item { Value = "RetGM", Text = "Rtlr Gross Margin ($)" },
        new Item { Value = "MfrGM", Text = "Mfr Gross Margin ($)" },
    };

    return View(model);
}

如果值是动态的(例如来自数据库或其他东西),通常您需要这样做。但如果它们是静态的,您可以直接将它们放在 View 模型的 getter 属性中:

public class OptimizeModels
{   
    public string SelectedItem { get; set; }
    public IEnumerable<Item> Items 
    {
        get
        {
            return new[] 
            {
                new Item { Value = "Sales", Text = "Units" },
                new Item { Value = "RetGM", Text = "Rtlr Gross Margin ($)" },
                new Item { Value = "MfrGM", Text = "Mfr Gross Margin ($)" },
            };
        }
    }
}

请注意,我已经删除了 Items 属性的 setter ,因为您不再需要为它分配一个值,无论是在 GET 操作中还是在 POST 操作中。

关于c# - 在 MVC 中使用下拉菜单时出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14055286/

相关文章:

c# - 获取 DropDownList 的选定值。 ASP.NET MVC

c# - 事件处理程序/引发程序代码片段

c# - sqlDecimal 到十进制 clr 存储过程无法将类型 'System.Data.SqlTypes.SqlDecimal' 的对象转换为类型 'System.IConvertible'

c# - 在 ASP.NET 5 中为 Web API Controller 模拟 HttpContext

c# - 静态是否意味着没有状态

c# - 异步任务。运行不工作

cordova - 将 ASP.NET MVC4 SPA AntiForgeryToken 转换为 PhoneGap

mysql - MVC 中结果不同时 SQL 返回相同结果两次

c# - 来自时区分钟偏移量的 TimeZoneInfo

asp.net-mvc - 将另一个 "Pet"添加到模型表单