Che*_*ton 0 vb.net asp.net-mvc html.listboxfor
我是这个ASP.net MVC的新手,并且真的陷入了ListBoxFor和DropDownListFor.
我该如何使用它们?任何例子?
这真的不是那么难.一如既往,您可以从定义视图模型开始:
Public Class MyViewModel
Public Property SelectedItems As IEnumerable(Of String)
Public Property SelectedItem As String
Public Property Items As IEnumerable(Of SelectListItem)
End Class
Run Code Online (Sandbox Code Playgroud)
然后一个控制器:
Public Class HomeController
Inherits System.Web.Mvc.Controller
Function Index() As ActionResult
Dim model = New MyViewModel 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(model)
End Function
Function Index(model As MyViewModel) As ActionResult
' Here you can use the model.SelectedItem which will
' return you the id of the selected item from the DropDown and
' model.SelectedItems which will return you the list of ids of
' the selected items in the ListBox.
...
End Function
End Class
Run Code Online (Sandbox Code Playgroud)
最后是一个相应的强类型视图:
@ModelType MvcApplication1.MyViewModel
@Using Html.BeginForm()
@Html.DropDownListFor(Function(x) x.SelectedItem, Model.Items)
@Html.ListBoxFor(Function(x) x.SelectedItems, Model.Items)
@<input type="submit" value="OK" />
End Using
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7925 次 |
| 最近记录: |