在MVC中使用下拉列表时出错?

时间:2012-12-27 13:10:35

标签: 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; }
    }

和我的控制器

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

任何消化?

2 个答案:

答案 0 :(得分:3)

HttpPost操作中,您忘记在渲染视图之前重新绑定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);
}

如果值是动态的(例如来自数据库或其他东西),通常需要这样做。但如果它们是静态的,你可以直接将它们放在视图模型的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操作中为其赋值。

答案 1 :(得分:2)

OptimizeModels.Items集合未在HTTP POST操作上传递给服务器。您需要显式设置它们并返回POST操作方法。