我正在尝试使用以下下拉框来选择要为模型编辑的值范围。
到目前为止,我已经得到了以下代码:
@Html.DropDownList("Services", "")
但本质上我想在这里保存该字符串而不是这个:
@Html.EditorFor(Function(model) model.ServiceName)
我的看法是:
@Using Html.BeginForm()
@Html.ValidationSummary(True)
@<fieldset>
<legend>RequestedService</legend>
<div class="editor-label">
@Html.LabelFor(Function(model) model.ServiceId)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.ServiceId)
@Html.DropDownList("Services", "")
@Html.ValidationMessageFor(Function(model) model.ServiceId)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
End Using
目前这两件事。
我的控制器:
Function AddService(id As Integer) As ViewResult
Dim serv As RequestedService = New RequestedService
serv.JobId = id
Dim ServiceList = New List(Of String)()
Dim ServiceQuery = From s In db.Services
Select s.ServiceName
ServiceList.AddRange(ServiceQuery)
ViewBag.Services = New SelectList(ServiceList)
Return View(serv)
End Function
最后是我的模型:
Imports System.Data.Entity
Imports System.ComponentModel.DataAnnotations
Public Class RequestedService
Public Property RequestedServiceId() As Integer
Public Property ServiceId() As Integer
<Required()>
<Display(Name:="Job Number *")>
Public Property JobId() As Integer
<Required()>
<Display(Name:="Station ID *")>
Public Property StationId() As Integer
End Class