在的SelectList MVC DropDownList中返回为空 [英] SelectList returns as null in MVC DropDownList

查看:478
本文介绍了在的SelectList MVC DropDownList中返回为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用MVC RC1窗体上的Html.DropDownList帮手的问题。

I'm having problems using the Html.DropDownList helper on a MVC RC1 form.

在控制器类,我创建一个这样的的SelectList

In the controller class, I create a SelectList like this

SelectList selectList = new SelectList(db.SiteAreas, "AreaId", "AreaName");
ViewData["AreaId"] = selectList;

或像这样

IEnumerable<SelectListItem> areaList =
    from area in db.SiteAreas
    select new SelectListItem
    {
        Text = area.AreaName,
        Value = area.AreaId.ToString(),
        Selected = false
    };
ViewData["AreaId"] = areaList;

,然后使用此调用其添加到浏览器

and then add it into the viewer using this call

<% using (Html.BeginForm())
   { %>
   <label for="sitearea">Site Area:</label>
   <span class="hint">The menu option to be highlighted when the page is open </span>
   <br />
   <%= Html.DropDownList("sitearea", (SelectList)ViewData["AreaId"], "Select Area Id")%>
   <%= Html.ValidationMessage("sitearea") %>
   <br />
   <br />
   <input type="submit" value="Add New Page" />
   <% = Html.AntiForgeryToken() %>
<% } %>

所以我加入一个默认选项为DROPDOWNLIST为好。如果我运行这个页面,并单击而不做任何的提交按钮,我预计POST操作此页火灾和这个下拉列表上验证code告诉我,我没有选择的选择。

So I'm adding a default option to the Dropdownlist as well. If I run this page and click the submit button without doing anything, I expected the POST action for this page to fire and for the validation code on this drop down list to tell me I haven't chosen an option.

相反,(MVC的来源$ C ​​$ C连接到我的项目)后,我发现我在selectextensions.cs得到一个ArgumentNullException。

Instead, (after attaching the MVC source code to my project), I find I'm getting an ArgumentNullException in selectextensions.cs.

$exception  {"Value cannot be null.\r\nParameter name: selectList"}	System.Exception {System.ArgumentNullException}

这涉及到一个名为SelectInternal方法,期望null以外的值其选择列表的参数。

This relates to a method called SelectInternal which is expecting a value other than null for its selectList parameter.

private static string SelectInternal(this HtmlHelper htmlHelper, string optionLabel, string name, IEnumerable<SelectListItem> selectList, bool usedViewData, bool allowMultiple, IDictionary<string, object> htmlAttributes) 
{
   if (String.IsNullOrEmpty(name)) {
      throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
   }
   if (selectList == null) {
      throw new ArgumentNullException("selectList");
   }  
.. rest of method ..

现在我所做的就是从升级MVC的测试1 code到RC1,突然这已止跌回升。我已经看到了在此之前SelectInternal方法,但没有具体的解决引用。因此,谁能帮我找出为什么这个错误发生,如何解决?

Now all I've done is upgrade the code from MVC beta 1 to RC1 and suddenly this has turned up. I've seen references to this SelectInternal method before, but no concrete resolution. So can anyone help me figure out why this error is occurring and how to fix it?

在此先感谢

推荐答案

这是不是在MVC一个bug,是由设计(据我所知)

This is not a bug in MVC and is by design (AFAIK)

您需要重新设置您的可视数据并返回模型回视图。

You need to re-set your viewdata and return the model back to the view.

视图数据只存在当前请求+ 1(即后回)。

The view data only exists for the current request + 1 (i.e the post back).

这篇关于在的SelectList MVC DropDownList中返回为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆