我試圖使用下面的下拉框來選擇一系列值來編輯模型。使用模型的下拉列表
到目前爲止,我已經得到了下面的代碼工作:
@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
你能告訴我們你的行爲和你的模型嗎? – Jorge 2012-08-13 15:54:38
編輯如上 – NickP 2012-08-13 18:38:58