1 Replies - 1324 Views - Last Post: 23 August 2016 - 01:33 PM

#1 fearfulsc2  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 169
  • Joined: 25-May 16

Invalid Operation Exception Error Happening in MVC

Posted 19 August 2016 - 09:35 PM

Hey Everyone, I'm trying to do a basic thing right now where I can add a student to the database. And the page loads just fine and I can enter in the information. When I go to submit that information, I get this type or error:

Additional information: The ViewData item that has the key 'Status' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.

However, the student I created is put inside of my database, so it was created after all. I just don't know why my program is crashing like this. Any ideas as to why?

Here are the snippets

Controller:
public ActionResult AddStudent([Bind(Include = "StudentID,FirstName,LastName,Status")] Student s)
      {
        var ed = new EFData();
        //ed.InsertStudent(new RegistrationMapper().ToEntity(sm));

        ed.InsertStudent(s);
        return View(s);
       }



Mapper
    public Student ToEntity(StudentModel sm)
    {
      return new Student()
      {
        FirstName = sm.StudentFirstName,
        LastName = sm.StudentLastName,
        Status = sm.Status
      };
    }



Model
  public class StudentModel
  {
    public int StudentID { get; set; }
    public string StudentFirstName { get; set; }
    public string StudentLastName { get; set; }
    public int Status { get; set; }
  }



view
@model SchoolRegistration.DataAccess.Student

@{
    ViewBag.Title = "AddStudent";
}

<h2>AddStudent</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Student</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
            </div>
        </div>

       <div class="form-group">
            @Html.LabelFor(model => model.Status, "Status", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("Status", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to Student List", "Index")
</div>

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>




For the View, this is the bit of code that is causing the program to crash and have that error
        @Html.LabelFor(model => model.Status, "Status", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("Status", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })



Would anyone know what it is that I am doing to break my program but yet still allow the submission to add into the database?

Is This A Good Question/Topic? 0
  • +

Replies To: Invalid Operation Exception Error Happening in MVC

#2 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Invalid Operation Exception Error Happening in MVC

Posted 23 August 2016 - 01:33 PM

Look carefully at this code.

 @Html.DropDownList("Status", null, htmlAttributes: new { @class = "form-control" })
 @Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })



Now, back to the error message.

The ViewData item that has the key 'Status' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.




If you take a look you are trying to create a drop down list based on model.Status and that is an integer. Creating a drop down list requires an IEnumerable, not an integer. You will need to switch to another control type or create a list of valid entries and then assign the selected value to be the status.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1