收到错误消息,“不存在带有键“原因”的 ViewData 项;类型为“IEnumerable”用于 asp.net mvc 中的 DropDownList

发布于 2024-10-17 10:33:07 字数 1503 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想念有你 2024-10-24 10:33:08

您应该从控制器返回 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.

各自安好 2024-10-24 10:33:07

因此,您有一个返回视图的控制器操作,但您没有将任何模型传递给该视图:

return View();

但是您正在尝试在视图中使用 Model.Reason 。这是不允许的。

因此,第一件事是让控制器操作将模型返回到视图:

public ActionResult Index()
{
    Appointment appointment = ... // get an appointment from somewhere
    // instantiate your view model
    var model = new AppointmentFormViewModel(appointment);
    // pass the view model to the view
    return View(model);
}

第二件事是在视图中使用强类型助手 (DropDownListFor),它使用标量类型属性作为第一个参数将包含下拉列表中选定的值(假设 Appointment.Reason 是一个简单的属性类型,如 string):

<%= Html.DropDownListFor(x => x.Appointment.Reason, Model.Reason, "-- Reason --") %>
<%= Html.ValidationMessageFor(x => x.Appointment.Reason, "*") %>

现在您可以摆脱 string ReasonValues 字典中的 .Empty 值,因为我们可以直接在视图中指定它。

So you have a controller action which returns a view but you are not passing any model to this view:

return 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:

public ActionResult Index()
{
    Appointment appointment = ... // get an appointment from somewhere
    // instantiate your view model
    var model = new AppointmentFormViewModel(appointment);
    // pass the view model to the view
    return View(model);
}

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 that Appointment.Reason is a simple property type like string):

<%= Html.DropDownListFor(x => x.Appointment.Reason, Model.Reason, "-- Reason --") %>
<%= Html.ValidationMessageFor(x => x.Appointment.Reason, "*") %>

And now you can get rid of the string.Empty value in your ReasonValues dictionary as we can specify it directly in the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文