0

I have created an MVC application where the user fills in a form of data. I am having a problem with retrieving the values that the user selected in the dropdownlists. I can successfully get all the other information from the page (for example the summary I can retrieve with is a textbox).

Here is what I have got so far:

ViewModel:

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

Here is my View:

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

Here is my Controller:

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

How can I successfully get the value of the ProjectTypeID from the list as this is not working?

Ciaran Donoghue
  • 800
  • 3
  • 22
  • 46
  • You have to add one property in model with Name ProjectTypeID then you'll get it or the other option is try with @Html.DropDownList("proTableList[0].ProjectTypeID",...... – Ravi Aug 20 '15 at 11:07
  • http://stackoverflow.com/questions/27901175/how-to-get-dropdownlist-selectedvalue-in-controller-in-mvc4 – A_Sk Aug 20 '15 at 11:33

1 Answers1

1

You have to add one property in model with the Name "ProjectTypeID" as int datatype(your actual data type) then you'll get it or the other option is try with:

@Html.DropDownList("proTableList[0].ProjectTypeID", New SelectList(Model.proTableList, "ProjectTypeID", "ProjectTypeName"), "Please Select")
Ciaran Donoghue
  • 800
  • 3
  • 22
  • 46
Ravi
  • 475
  • 4
  • 12
  • where do you use the ProjectTypeID from the Model in the view then? – Ciaran Donoghue Aug 20 '15 at 11:20
  • You have property Public Property proTableList As List(Of ProjectType) in class ClientUserProjectIssue so in SubmitIssue function you will get the value as test.proTableList (0).ProjectTypeID for the second option if you use 1st option then directly you can get the value as test.ProjectTypeID Let me know if it make sense – Ravi Aug 20 '15 at 11:29
  • 1
    Other thing you can do is @Html.DropDownList("IssueTable.ProjectTypeID", New SelectList(Model.proTableList, "ProjectTypeID", "ProjectTypeName"), "Please Select") then you can get value directly as issTable.ProjectTypeID = test.IssueTable.ProjectTypeID – Ravi Aug 20 '15 at 11:40