1

在这里,我正在使用下拉列表并收到类似的错误

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

收到此错误是因为在发布期间我获得了项目的空值。我已经在 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);
        }

有什么建议吗?

4

2 回答 2

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 属性的设置器,因为您不再需要为它分配值,无论是在您的 GET 操作还是在 POST 操作中。

于 2012-12-28T06:28:32.073 回答
2

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

于 2012-12-27T13:13:48.903 回答