0

I'm brand new to MVC on the ASP.NET platform and I'm having an issue with trying to create an Html form that uses a drop down list as one of the options. I've successfully been able to use the form when the drop down list isn't using it's default value. But, I've been doing some testing and when I leave the drop down list empty it throws a couple of errors.

An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code

Additional information: There is no ViewData item of type 'IEnumerable' that has the key 'CraigRole'.

The code that I am using.

Controller:

public ActionResult Create()
{
    List<SelectListItem> items = new List<SelectListItem>();
    SelectListItem item1 = new SelectListItem() { Value = "User", Text   = "User" };
    items.Add(item1);
    SelectListItem item2 = new SelectListItem() { Value = "Admin", Text = "Admin" };
    items.Add(item2);
    SelectListItem item3 = new SelectListItem() { Value = "Super Admin", Text = "SuperAdmin" };
    items.Add(item3);
    ViewData["CraigRole"] = items;
    return View();
}

View:

<div class="form-group">
    @Html.LabelFor(model => model.CraigRole, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.CraigRole, (List<SelectListItem>)ViewData["CraigRole"], "--- Please Select ---", new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.CraigRole, "", new { @class = "text-danger" })
    </div>
</div>

I have never had experience with Validation in MVC, so I apologize if I am missing something. I also would like to point out that I have seen many questions regarding drop down lists on stack exchance (that is how I've gotten this far), but I can't find a solution for this problem. Any help is appreciated!.

Pinball125
  • 25
  • 1
  • 4
  • You cannot use the same name for the property your binding to and the `SelectList` (change it to say`ViewData["CraigRoleList"] = items;` - refer also [this answer](http://stackoverflow.com/questions/37161202/will-there-be-any-conflict-if-i-specify-the-viewbag-name-to-be-equal-to-the-mode/37162557#37162557) –  Nov 25 '16 at 09:46
  • Thanks Stephen, I've gone and changed this, and I'm getting the same errors when I submit the form. I apologize, I didn't see the second part of your answer linking to your other answer. Will look into this further, thanks! – Pinball125 Nov 25 '16 at 09:50
  • Thanks for the help Stephen! It turned out to be that I wasn't recreating the List inside the post section of the create action. I've done this and it continued to throw errors, so I've added some data annotations to the model and everything looks good now. I also wanted to point out to you that it looks like your dot net fiddle link in the other answer appears to be down. Again, thanks for the help! – Pinball125 Nov 25 '16 at 10:03
  • Its fine just now :) - but this time of day it can be a bit slow due to load –  Nov 25 '16 at 10:06

0 Answers0