收到错误消息,“不存在带有键“原因”的 ViewData 项;类型为“IEnumerable”用于 asp.net mvc 中的 DropDownList
我对 ASP.net 框架非常陌生,如果有人能帮助我找出我在这里做错了什么,我将不胜感激。
我正在尝试使用 asp.net mvc 3.5 (C#) 从 SelectList 创建表字段“Reason”的下拉列表。
在我的视图代码中,我使用了 MVC HTML 方法 Html.DropdownList:
<%= Html.DropDownList("Reason", Model.Reason)%>
<%= Html.ValidationMessage("Reason", "*")%>
在模型类中,我在代码中添加了“原因”列表:
namespace Appointments.Models
{
public class AppointmentFormViewModel
{
public Appointment Appointment { get; set; }
public Dictionary<string, string> ReasonValues
{
get
{
return new Dictionary<string, string>()
{
{ string.Empty, string.Empty },
{ "Add/Drop", "Add/Drop a Course" },
{ "Academic Diff.", "Academic Difficulty" },
{ "Academic Plan", "Academic Plan" },
{ "Other", "Other: Fill out below" }
};
}
}
public SelectList Reason { get; set; }
public AppointmentFormViewModel(Appointment appointment)
{
Appointment = appointment;
Reason = new SelectList(ReasonValues, "Key", "Value", appointment.Reason);
}
在控制器中,代码是:
public ActionResult Index()
{
ViewData["Reason"] = new SelectList("ReasonValues");
return View();
}
当我调试代码时,我收到一条错误消息, '不存在键为“IEnumerable”类型的“Reason”的 ViewData 项。
谢谢你!
I'm very new to the ASP.net framework, and will appreciate it if somebody could help me to figure out what I'm doing wrong here.
I'm trying to create a dropdownlist for the table field, "Reason" from a SelectList using asp.net mvc 3.5 (C#).
In my View code I used MVC HTML method Html.DropdownList:
<%= Html.DropDownList("Reason", Model.Reason)%>
<%= Html.ValidationMessage("Reason", "*")%>
In the Model class I added the list of 'reasons' in the code:
namespace Appointments.Models
{
public class AppointmentFormViewModel
{
public Appointment Appointment { get; set; }
public Dictionary<string, string> ReasonValues
{
get
{
return new Dictionary<string, string>()
{
{ string.Empty, string.Empty },
{ "Add/Drop", "Add/Drop a Course" },
{ "Academic Diff.", "Academic Difficulty" },
{ "Academic Plan", "Academic Plan" },
{ "Other", "Other: Fill out below" }
};
}
}
public SelectList Reason { get; set; }
public AppointmentFormViewModel(Appointment appointment)
{
Appointment = appointment;
Reason = new SelectList(ReasonValues, "Key", "Value", appointment.Reason);
}
In the Controller the code is:
public ActionResult Index()
{
ViewData["Reason"] = new SelectList("ReasonValues");
return View();
}
When I'm debugging the code I'm getting an error message, 'There is no ViewData item with the key 'Reason' of type 'IEnumerable'.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该从控制器返回 AppointmentFormViewModel 的实例。然后,您应该创建一个强类型的视图(也是 AppointmentFormViewModel 类型)。然后您可以在您的视图中使用:Model.ReasonValues。
You should return an instance of AppointmentFormViewModel from you controller. Then you should create a view that is strongly typed (of type AppointmentFormViewModel as well). Then you can use: Model.ReasonValues in your view.
因此,您有一个返回视图的控制器操作,但您没有将任何模型传递给该视图:
但是您正在尝试在视图中使用
Model.Reason
。这是不允许的。因此,第一件事是让控制器操作将模型返回到视图:
第二件事是在视图中使用强类型助手 (
DropDownListFor
),它使用标量类型属性作为第一个参数将包含下拉列表中选定的值(假设Appointment.Reason
是一个简单的属性类型,如string
):现在您可以摆脱
string
值,因为我们可以直接在视图中指定它。ReasonValues
字典中的 .EmptySo you have a controller action which returns a view but you are not passing any model to this view:
And yet your are trying to use
Model.Reason
in your view. That's not allowed.So the first thing is to have your controller action return a model to the view:
The second thing is to use a strongly typed helper in your view (
DropDownListFor
) which uses as first argument a scalar type property that will harbor the selected value of the dropdown (assuming here thatAppointment.Reason
is a simple property type likestring
):And now you can get rid of the
string.Empty
value in yourReasonValues
dictionary as we can specify it directly in the view.