gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:20:13 24 4
gpt4 key购买 nike

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

的错误
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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com