0

I have a dropdown list that is populating correctly on the page load but when I select a choice and submit it back with data from additional fields I am getting this error. Do I need to reset it back to the default value? In the below code "Industries" is a List of type SelectListItem.

@using (Html.BeginForm())
{
    @Html.DropDownListFor(n => n.SelectedItem, Model.Industries)
     <br>
      //MORE CONTROLS 
     <br>
     <input type="submit" value="Calculate" />
 }

[HttpGet]
public ActionResult Calculate()
{
    ClassName DataStuff = new ClassName ();
    return View(new ClassName { Industries = DataStuff.IndustryList() 
});
}

[HttpPost]
public ActionResult Calculate(ClassName  Items)
{
 ClassName CompleteItems = RetrieveData(Items);  
      // above additional calculations 
        return View(CompleteItems);
}

The model (with the relevant fields) is:

public class ClassName
{

    public string SelectedItem { get; set; }
    public IEnumerable<SelectListItem> Industries { get; set; }
    public List<SelectListItem> IndustryList()
    {
       //populating the DDL
    }
}
Borges
  • 17
  • 6

1 Answers1

-1

1. Define Industries as IEnumerable<SelectListItem> or

if you defined Industries as List<SelectListItem> cast Industries to IEnumerable like

(IEnumerable<SelectListItem>)Model.Industries

2.In your model ,in place of public string SelectedItem { get; set; } use

public string Industry{ get; set; } and

in view update with new property use @Html.DropDownListFor(n => n.Industry, Model.Industries)

user9405863
  • 1,506
  • 1
  • 11
  • 16