Reputation: 11010
here i am using a dropdownlist and getting an error like
ArgumentNullException was unhandled by user code
Value cannot be null.
Parameter name: items
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")})
and my model
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; }
}
and my 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);
}
Any suugestion?
Upvotes: 1
Views: 225
Reputation: 1038820
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);
}
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 ($)" },
};
}
}
}
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.
Upvotes: 3
Reputation: 58444
OptimizeModels.Items
collection doesn't get passed to the server on the HTTP POST action. You need to explicitly set them and return on your POST action method, too.
Upvotes: 2