次のドロップダウン ボックスを使用して、モデルに対して編集する値の範囲を選択しようとしています。
これまでのところ、次のコードが機能するようになりました。
@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