问题描述
我绝对是初学者。
型号:
Partial Public Class datamart
<StringLength(18)>
Public Property CollaboartionRequestIdSfdcBk As String
<Key>
<StringLength(20)>
Public Property Name As String
查看:
<div class="form-group">
@Html.LabelFor(Function(model) model.ProductName,htmlAttributes:=New With {.class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(Function(model) model.ProductName,New With {.htmlAttributes = New With {.class = "form-control"}})
@Html.ValidationMessageFor(Function(model) model.ProductName,"",New With {.class = "text-danger"})
</div>
</div>
我很确定涉及下拉列表之类的东西,但是数据选项从哪里来??
解决方法
您的模型与视图不匹配,您使用的是 ProductName 还是 Name ?这是一个运行良好的代码示例,试试吧:
在 DropDownList 和 ViewData 中使用 Html Helper:
在你的控制器( HomeController.vb )中:
Public Function ShowMyDropDown() As ActionResult
Dim List1 As List(Of SelectListItem) = New List(Of SelectListItem) From {
New SelectListItem With {.Text = "Red",.Value = 1},New SelectListItem With {.Text = "Green",.Value = 2},New SelectListItem With {.Text = "Blue",.Value = 3}
}
ViewData("List1") = New SelectList(List1,"Value","Text",2) '2 is default selected value
Return View()
End Function
在您的视图中 (ShowMyDropDown.vbhtml):
@Html.DropDownList("Colors",TryCast(ViewData("List1"),SelectList),"Select a color",htmlAttributes:=New With {Key .class = "form-control"})
这个例子只是你可以开始了解基础,然后尝试解释更多你需要的东西,如果你不能达到你的需要,你需要用模型我会添加一个编辑。