0

I am receiving the error message above. I have looked at other's issues and can't seem to pinpoint the error. Here is the code that I have for the controller. Here is the error that I am getting :

System.InvalidOperationException: The ViewData item that has the key 'RecieverID' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'

public ActionResult Create()
    {
        ViewBag.RecieverID = new SelectList(db.AspNetUsers, "Id", "Email");
        return View();
    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "RecieverID,TEXT")] Message message)
    {
        if (ModelState.IsValid)
        {
            var mes = new Message
            {
                SenderID = User.Identity.GetUserId(),
                DATETIMEMADE = DateTime.Now,
                TEXT = message.TEXT
            };
            db.Messages.Add(mes);
            db.SaveChanges();
            return RedirectToAction("Index");
         }
        ViewBag.ReceiverID = new SelectList(db.AspNetUsers, "Id", "Email", message.RecieverID);
        return View(message);
    }

After that here is my view for creating a message:

@model MUE.Models.Message

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Message</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })


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



        <div class="form-group">
            @Html.LabelFor(model => model.TEXT, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.TEXT, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.TEXT, "", 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 List", "Index")
</div>

Not sure where the issue is coming from but any help would be greatly appreciated.

mason
  • 31,774
  • 10
  • 77
  • 121

2 Answers2

0

First off, and not to be a grammar Nazi, but it should be "Receiver" not "Reciever".

Second, model.RecieverID indicates a single field, not a collection.

"new SelectList(..." is collection.

Please refer to this: MVC - Set selected value of SelectList

Community
  • 1
  • 1
Kevin Raffay
  • 842
  • 5
  • 18
  • Yeah, I didn't do the database. It is a group project and someone else had the job of doing the database. I noticed that too. But thanks for the link! – Jacob Staggs Apr 04 '17 at 16:26
0

You could create a ViewModel for the page such as:

public class MyViewModel
{
    public SelectList ReceiverList
    public MUE.Models.Message ReceivedMessage
}

Assign your values in the controller and then do something like:

@Html.DropDownListFor(model => model.ReceivedMessage.ReceiverID, Model.ReceiverList, new { @class = "form-control" })
McGaz
  • 1,354
  • 1
  • 13
  • 22