我收到上述錯誤信息。我看了其他的問題,似乎無法查明錯誤。這裏是我對控制器的代碼。下面是我得到的錯誤:System.InvalidOperationException:當提交消息
System.InvalidOperationException:具有關鍵的ViewData的項目「RecieverID」的類型「System.String」的,但必須是類型「的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);
}
,這裏以後就是我創建消息視圖:
@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>
不知道在哪裏的問題來自但任何幫助將不勝感激。
什麼是在堆棧跟蹤和異常中的InnerException錯誤? –