开发者

I got an error "There is no ViewData item of type 'IEnumerable<SelectListItem>'

I got an error "There is no ViewData item of type 'IEnumerable' that has the key 'TIMEZONE'".

View

        <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inher开发者_开发知识库its="System.Web.Mvc.ViewPage<EventListing.Models.EventInfo>" %>               
        <%= Html.DropDownList("TIMEZONE", (IEnumerable<SelectListItem>)ViewData["TIMEZONE"], "Select Timezone")%>

Controller

ViewData["TIMEZONE"] = new SelectList(EventModel.getTIMEZOMES, "Key", "Value");
return View();

Model Page

public static IList<KeyValuePair<string, string>> getTIMEZOMES
        {
            get
            {
                Dbhelper DbHelper = new Dbhelper();
                IList<KeyValuePair<String, String>> Timezone = new List<KeyValuePair<String, String>>();
                DbCommand cmd = DbHelper.GetSqlStringCommond("SELECT * FROM TMP_TIMEZONES");
                DbDataReader Datareader = DbHelper.ExecuteReader(cmd);
                while (Datareader.Read())
                {
                    Timezone.Add(new KeyValuePair<String, String>(Datareader["ABBR"].ToString(), Datareader["NAME"].ToString()));
                }

                return Timezone;
            }
        }

Plz Give the solution.


You should not use the same property for binding the dropdownlist to and the selectlist. They should be distinct. The first argument must represent a scalar property which will be used as name of the generated select. The second argument must be the list:

<%= Html.DropDownList(
    "SelectedTimeZone", 
    (IEnumerable<SelectListItem>)ViewData["TIMEZONE"], 
    "Select Timezone"
) %>

This will use the collection contained in ViewData["TIMEZONE"] to generate the select like this:

<select name="SelectedTimeZone">
    <option value="1">zone 1</option>
    ...
</select>

When the form is submitted you will look for the SelectedTimeZone property in the request to fetch the corresponding selected value.

This being said I notice that your view is strongly typed to some model:

Inherits="System.Web.Mvc.ViewPage<EventListing.Models.EventInfo>"

but you aren't passing any model when returning this view:

ViewData["TIMEZONE"] = new SelectList(EventModel.getTIMEZOMES, "Key", "Value");
return View(); // <!-- where is the Model???

So what I would recommend you is to use a view model:

public class MyViewModel
{
    public SelectList TimeZones { get; set; }
    public string SelectedTimeZone { get; set; }

    // ... include some other properties that might be
    // needed by this view
}

and then have your controller action populate this view model:

public ActionResult Index()
{
    var model = new MyViewModel
    {
        TimeZones = new SelectList(EventModel.getTIMEZOMES, "Key", "Value")
    };
    return View(model);
}

and finally in your strongly typed view use a strongly typed DropDownListFor helper:

<%@ Page 
    Title="" 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<EventListing.Models.MyViewModel>" 
%>

<%= Html.DropDownListFor(
    x => x.SelectedTimeZone, 
    Model.TimeZones, 
    "Select Timezone"
) %>


One possible reason could be, In the controller, you may not be getting the data into the ViewData["TimeZone"]. Try to debug it and see what you are getting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜