C# System.InvalidOperationException:提交邮件时
我收到上面的错误消息。我看了别人的问题,似乎无法找出错误。这是我为控制器准备的代码。以下是我得到的错误: System.InvalidOperationException:具有键“ReceiverId”的ViewData项的类型为“System.String”,但其类型必须为“IEnumerable” 之后,这里是我创建消息的视图:C# System.InvalidOperationException:提交邮件时,c#,asp.net,C#,Asp.net,我收到上面的错误消息。我看了别人的问题,似乎无法找出错误。这是我为控制器准备的代码。以下是我得到的错误: System.InvalidOperationException:具有键“ReceiverId”的ViewData项的类型为“System.String”,但其类型必须为“IEnumerable” 之后,这里是我创建消息的视图: @model MUE.Models.Message @{ ViewBag.Title = "Create"; } <h2>Create<
@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>
@model MUE.Models.Message
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
消息
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.RecieverID,“RecieverID”,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownList(“RecieverID”,null,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.RecieverID,“,new{@class=“text danger”})
@LabelFor(model=>model.TEXT,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.TEXT,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.TEXT,“,new{@class=“TEXT danger”})
}
@ActionLink(“返回列表”、“索引”)
不确定问题来自何处,但如果您能提供任何帮助,我们将不胜感激。首先,不要成为语法纳粹,但应该是“接受者”而不是“接受者” 其次,model.RecieverID表示单个字段,而不是集合 “新建选择列表(…”是一个集合 请参阅:
您可以为页面创建ViewModel,例如:
public class MyViewModel
{
public SelectList ReceiverList
public MUE.Models.Message ReceivedMessage
}
在控制器中指定值,然后执行以下操作:
@Html.DropDownListFor(model => model.ReceivedMessage.ReceiverID, Model.ReceiverList, new { @class = "form-control" })
stacktrace上显示了什么,以及你的异常中的InnerException错误?是的,我没有做数据库。这是一个团队项目,其他人负责做数据库。我也注意到了。但是谢谢你的链接!
@Html.DropDownListFor(model => model.ReceivedMessage.ReceiverID, Model.ReceiverList, new { @class = "form-control" })