收到一个错误,而在MVC使用下拉菜单? [英] Getting an error while using dropdown in MVC?

查看:146
本文介绍了收到一个错误,而在MVC使用下拉菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我使用一个DropDownList,并得到一个错误,如

here i am using a dropdownlist and getting an error like

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

我得到这个错误,因为在后我得到了项目空值。
我曾尝试在MVC 这个样本下拉
这里是我的下拉

Am getting this error because during post am getting a null value for item. I have tried this sample Dropdown in MVC Here is my dropdown

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

任何suugestion?

Any suugestion?

推荐答案

在你的 HttpPost 操作您忘记渲染视图之前重新绑定下拉的值。因为收藏是永远不会发布到服务器,您需要填充你在你的GET操作一样的工作方式:

In your HttpPost action you forgot to rebind the values of the DropDown before rendering the view. Since the collection is never POSTed to the server you need to populate it the same way you did in your GET action:

[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属性:

Normally you need to do that if the values are dynamic (for example coming from a database or something). But if they are static you could simply put them in the getter property of your view model directly:

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属性,你不再需要它分配一个值,无论是在你付诸行动,也没有在POST操作的通知。

Notice that I have removed the setter for the Items property as you no longer need to assign it a value, neither in your GET action nor in the POST action.

这篇关于收到一个错误,而在MVC使用下拉菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆