在MVC项目中,我试图在下拉列表中绑定选定的值。 例如,对于AUTHOR_ID字段,文本字段将使用值和下拉列表进行归档,但所选值不会设置。
我正在使用以下类(模型)。
Public Class ProfileCard
Public Property SearchIndex As Integer
<Key()>
Public Property DOCNUM As String
Public Property DOCNAME As String
Public Property TYPIST_ID As String
Public Property AUTHOR_ID As String
Public Property TYPE_ID As String
Public Property LAST_EDIT_ID As String
Public Property APP_ID As String
Public Property CREATION_DATE As String
Public Property LASTEDITDATE As String
Public Property ABSTRACT As String
Public Property postedfile As HttpPostedFileBase
Public Property profileError As String
'listbox items
Public Property GetAuthors As IEnumerable(Of SelectListItem)
Public Property GetApplications As IEnumerable(Of SelectListItem)
'list with profile defaults
Public Property ListofProfileDefaults As List(Of ProfileDefaults)
End Class
Public Class ProfileDBContext
Inherits DbContext
Public Property ProfileCards() As DbSet(Of ProfileCard)
End Class
Public Class ProfileDefaults
Public Property id As String
Public Property desc As String
Public Property type As String
End Class
这是控制器中的编辑功能
Function Edit(ByVal id As Integer) As ActionResult
Dim ds As New ProfileCard
Dim selectpeople = (From t In db.PEOPLE
Select New SelectListItem With {.Text = t.USER_ID.ToString, .Value = t.USER_ID.ToString})
Dim selectapplications = (From t In db.APPS
Select New SelectListItem With {.Text = t.APPLICATION.ToString, .Value = t.APPLICATION.ToString})
ds.GetAuthors = selectpeople
ds.GetApplications = selectapplications
'list profile defaults!
ds.ListofProfileDefaults = Session("_lProfileDefaults")
For Each p As System.Reflection.PropertyInfo In ds.GetType().GetProperties()
If p.CanRead Then
If Not UCase((";SearchIndex;postedfile;GetAuthors;GetApplications;ListofProfileDefaults;")).Contains(";" & UCase(p.Name) & ";") Then
Dim retval As Type = p.PropertyType
Dim answer As Type = Nullable.GetUnderlyingType(retval)
Dim value As Object
If answer Is Nothing Then
value = System.Convert.ChangeType("Some value", retval)
Else
value = System.Convert.ChangeType("Some value", answer)
End If
p.SetValue(ds, value, Nothing)
End If
End If
Next
Return View(New List(Of ProfileCard)() From {ds})
End Function
这是视图中的代码。
@ModelTYPE IEnumerable(Of DMMvcApplication.ProfileCard)
@Using Html.BeginForm()
@Html.ValidationSummary(True)
@<fieldset>
@For Each item In Model
Dim listofrProfileDefaults = item.ListofProfileDefaults
@For Each a In listofrProfileDefaults
Select Case UCase(a.id)
Case "DOCNUM"
@Html.HiddenFor(Function(modelItem) item.DOCNUM)
Case "DOCNAME"
@Html.Label(a.desc)
@Html.EditorFor(Function(modelitem) item.DOCNAME)
Case "AUTHOR_ID"
@Html.Label(a.desc)
@Html.EditorFor(Function(modelitem) item.AUTHOR_ID)
@Html.DropDownListFor(Function(modelitem) item.AUTHOR_ID, item.GetAuthors, New With {.class = "chosenDropDown"})
Case "CREATION_DATE"
@Html.Label(a.desc)
@Html.TextBoxFor(Function(modelitem) item.CREATION_DATE, "{0:d MMM yyyy}", New With {.class = "datecss"})
Case "APP_ID"
@Html.Label(a.desc)
@Html.DropDownListFor(Function(modelitem) item.APP_ID, item.GetApplications, New With {.class = "chosenDropDown"})
End Select
Next
@<p>
</p>
Next
<p>
<input type="submit" value="Αλλαγή" />
</p>
</fieldset>
@<div id="dialog_lookup" style="display: none">
</div>
End Using
<div>
@Html.ActionLink("Επιστροφή", "Index", "SearchResults")
</div>
谢谢, Thanasis。
我想我解决了这个问题:
我有:
@Html.DropDownListFor(Function(modelitem) item.AUTHOR_ID, item.GetAuthors, New With {.class = "chosenDropDown"})
我把它改成了这个:
@Html.DropDownListFor(Function(modelitem) item.AUTHOR_ID, New SelectList(item.GetAuthors, "Value", "Text", item.AUTHOR_ID), New With {.class = "chosenDropDown"})
关键是具有Value和Text属性的新SelectList。