Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC RC(刷新)中的Html.DropDownList未预选项_C#_.net_Asp.net Mvc - Fatal编程技术网

C# ASP.NET MVC RC(刷新)中的Html.DropDownList未预选项

C# ASP.NET MVC RC(刷新)中的Html.DropDownList未预选项,c#,.net,asp.net-mvc,C#,.net,Asp.net Mvc,在我的控制器中,我有以下功能: ViewData["myList"] = new SelectList(itemRepository.GetAll(), "Id", "Name", currentItem.Id); 我认为: <%= Html.DropDownList("myItem", (SelectList)ViewData["myList"])%> 呈现的下拉列表应预先选择Id为currentItem.Id的项,但它没有。未选择任何内容,因此默认为第一个 这在我

在我的控制器中,我有以下功能:

ViewData["myList"] = 
   new SelectList(itemRepository.GetAll(), "Id", "Name", currentItem.Id);
我认为:

<%= Html.DropDownList("myItem", (SelectList)ViewData["myList"])%>

呈现的下拉列表应预先选择Id为currentItem.Id的项,但它没有。未选择任何内容,因此默认为第一个


这在我更新到RC/RC(刷新)之前就起作用了。有什么想法吗?

以下内容适合我(使用MVC RC刷新)

他认为:

<%= Html.DropDownList("NAME_OF_ITEM_ID_PROPERTY", (SelectList)ViewData["myList"]) %>

因此,对于您的示例,可能是:

<%= Html.DropDownList("Id", (SelectList)ViewData["myList"]) %>

如果将ViewData中键的名称与视图中表单字段的名称相匹配,则HTMLHelper设计为基于该键隐式从ViewData中提取。我建议将您的视图代码更改为:

<%= Html.DropDownList("myList") %>
下拉列表(“myItem”,(SelectList)ViewData[“myList”]) 下拉列表

List(字符串名,IEnumerable selectList)->
DropDownList(名称、选择列表、null/*对象、htmlAttributes*/)->
下拉列表(名称、选择列表、新RouteValueDictionary(htmlAttributes))->
SelectInternal(null、name、selectList、false、false、htmlAttributes)
因此,在一天结束时,这两条路径之间的区别在于,有效的方式将true传递到SelectInternal的usedViewData参数中,而不起作用的方式将传递false


在我看来,当usedViewData为false时,SelectInternal中的某个地方可能存在错误。

我只是创建了自己的下拉帮助程序。也许没有内置的那么高效,但它确实可以工作。

嗨,凯,这不就是名字和id的“id”吗?这将是错误的,因为我需要的名称和id是“myItem”,因为这是表单提交要查找的id。您尝试过吗?同时检查呈现的HTML。按照我的理解,在控制器中,您将值字段定义为项目模型的ID,将名称定义为SelectListItem的值。因此,通过将下拉列表中的第一个参数设置为Id,您可以请求这个值字段。是的,我尝试了。第一个参数始终是渲染下拉列表的名称。这不是给你的吗?接受这个答案。但是@Haacked也希望能看到它。@Troy顺便说一句-我应该清楚的是,多重问号并不是由MVC霸主直接指向你的。这仍然是我在MVC 1.0中面临的一个问题
SelectInternal( string optionLabel, string name, IEnumerable<SelectListItem> selectList, bool usedViewData, bool allowMultiple, IDictionary<string,object> htmlAttributes )
DropDownList( string name ) ->
SelectInternal( null, name, htmlHelper.GetSelectData(name), true, false, null )
List( string name, IEnumerable<SelectListItem> selectList ) ->
DropDownList( name, selectList, null /* object, htmlAttributes */ ) ->
DropDownList( name, selectList, new RouteValueDictionary(htmlAttributes) ) ->
SelectInternal( null, name, selectList, false, false, htmlAttributes )