从mvc中的viewModel中检索selectedList项的ID

时间:2015-08-20 11:02:56

标签: asp.net asp.net-mvc

我创建了一个MVC应用程序,用户填写了一种数据形式。我在检索用户在下拉列表中选择的值时遇到问题。我可以成功地从页面获取所有其他信息(例如,我可以检索的摘要是文本框)。

这是我到目前为止所得到的:

视图模型:

Public Class ClientUserProjectIssue
    Public Property proTableList As List(Of ProjectType)
    Public Property IssueTable As IssueTable
End Class

这是我的观点:

@ModelType IssueTracker.ClientUserProjectIssue
@Html.DropDownList("ProjectTypeID", New SelectList(Model.proTableList, "ProjectTypeID", "ProjectTypeName"), "Please Select")
@Html.EditorFor(Function(model) model.iTable.IssueSummary)

这是我的控制器:

Public Function SubmitIssue(test As IssueTracker.ClientUserProjectIssue) As ActionResult
    Dim issTable As New IssueTable
    issTable.IssueSummary = test.IssueTable.IssueSummary
    issTable.ProjectTypeID = 1 
   'issTable.ProjectTypeID = test.IssueTable.ProjectTypeID (this is what I would like to do, but it doesn't get the ID
    Using db As New DatabaseEntities
        db.IssueTables.Add(issTable)
        db.SaveChanges()
    End Using

    Return RedirectToAction("SubmitSuccess")
End Function

如何从列表中成功获取ProjectTypeID的值,因为这不起作用?

1 个答案:

答案 0 :(得分:1)

您必须在模型中添加一个名为“ProjectTypeID”的属性作为int数据类型(您的实际数据类型)然后您将获得它或其他选项尝试使用:

@Html.DropDownList("proTableList[0].ProjectTypeID", New SelectList(Model.proTableList, "ProjectTypeID", "ProjectTypeName"), "Please Select")