Dropdownlistfor MVC ASP.Net VB [英] Dropdownlistfor MVC ASP.Net VB
问题描述
我将数据发送到我的dropdownlistfor,我可以选择的数据,但是当我点击按钮即可进入POST方法我得到错误:
类型的异常'System.InvalidOperationException'发生在
System.Web.Mvc.dll程序,但在用户code没有处理
更多信息:具有关键的ViewData的项目
'的SelectedItem'的类型'System.String'的,但必须是类型
IEnumerable的'。
块引用>我认为在dropdownlistfor剃须刀视图中的第一个值是所选择的值,第二个是值显示的列表中,但现在我不知道该下拉列表是如何工作的; /
你能帮助我吗?
我的模型code:
公共类NewProcessIndex 公共属性名称作为字符串
公共属性DataSource1作为字符串 公共财产的SelectedItem作为字符串
公共属性Items为IEnumerable(中SelectListItem)末级我的控制器code:
<&HTTPGET GT;
功能AddNewProces()为的ActionResult 昏暗listBoxData =新NewProcessIndex随着{
.Items = {
新SelectListItem(){凭借= .value的1。文本=项目1},
新SelectListItem(){凭借= .value的2。文本=项目2},
新SelectListItem(){凭借= .value的3。文本=3项}
}
} 返回查看(listBoxData)
结束功能< HttpPost>
功能AddNewProces(newProcesData作为NewProcessIndex)作为的ActionResult newProcesData.DataSource1 = newProcesData.SelectedItem 返回查看(newProcesData)
结束功能和最后我的看法code:
@ModelType NewProcessIndex< H2> AddNewProces< / H>
@using(Html.BeginForm(AddNewProces,管理,FormMethod.Post)) @< DIV CLASS =形横> @ Html.LabelFor(功能(M)m.Name)
@ Html.TextBoxFor(功能(M)m.Name)
@ Html.ValidationMessageFor(功能(M)m.Name) @ Html.DropDownListFor(功能(X)x.SelectedItem,Model.Items) <输入类型=提交值=AddNewProces/>
< / DIV>使用完@If FormMethod.Post然后
@&所述p为H.; @ Model.Name&下; / P>
@&所述p为H.; @ Model.DataSource2&下; / P>
万一编辑:
有趣的事情......在dropdownlistfor不工作,但listboxfor这种形式的作品:@ Html.ListBoxFor(功能(X)x.SelectedItem,Model.Items)
解决方案大概是事实,我正在返回一个模型视图是一个问题,或与
一个有趣的我@If isPost然后
...
万一我不知道是什么导致了错误,但现在我使用ListBoxFor,一切工作像它应该....奇怪:)如果还有人想解释一下我为什么不能按预期工作(以及我期望它的工作),然后回答我马上:)
I send data to my dropdownlistfor and i can choose that data but when i click button to go to post method i get error:
"An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code
Additional information: The ViewData item that has the key 'SelectedItem' is of type 'System.String' but must be of type 'IEnumerable'."
I thought that the first value in razor view in dropdownlistfor is the selected value and second is list of values to show but now i have no idea how the dropdown list works ;/
could you help me?
My model code:
Public Class NewProcessIndex Public Property Name As String Public Property DataSource1 As String Public Property SelectedItem As String Public Property Items As IEnumerable(Of SelectListItem) End Class
My controller code:
<HttpGet> Function AddNewProces() As ActionResult Dim listBoxData = New NewProcessIndex With { .Items = { New SelectListItem() With {.Value = 1, .Text = "item 1"}, New SelectListItem() With {.Value = 2, .Text = "item 2"}, New SelectListItem() With {.Value = 3, .Text = "item 3"} } } Return View(listBoxData) End Function <HttpPost> Function AddNewProces(newProcesData As NewProcessIndex) As ActionResult newProcesData.DataSource1 = newProcesData.SelectedItem Return View(newProcesData) End Function
And Finally my view code:
@ModelType NewProcessIndex <h2>AddNewProces</h2> @Using (Html.BeginForm("AddNewProces", "Admin", FormMethod.Post)) @<div class="form-horizontal"> @Html.LabelFor(Function(m) m.Name) @Html.TextBoxFor(Function(m) m.Name) @Html.ValidationMessageFor(Function(m) m.Name) @Html.DropDownListFor(Function(x) x.SelectedItem, Model.Items) <input type="submit" value="AddNewProces" /> </div> End Using @If FormMethod.Post Then @<p>@Model.Name</p> @<p>@Model.DataSource2</p> End If
EDIT: funny thing ... the dropdownlistfor doesn't work but listboxfor works in this form:
@Html.ListBoxFor(Function(x) x.SelectedItem, Model.Items)
解决方案Probably the fact that i am returning a model to the view was an issue or me having a fun with
@If isPost Then ... End If
I am not sure what caused the error but now I am using ListBoxFor and everything works like it should.... strange :) If anyone still would like to explain me why it doesn't work as expected (well as i expect it to work) then answer me right away :)
这篇关于Dropdownlistfor MVC ASP.Net VB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!