Reputation: 377
Hi I am new to MVC and have looked through a great number of these posts and have not been able to solve my problem.
I have successfully populated my drop down list using a dropdownlistfor html helper, however when I submit the form the selected items value is not being passed with the model into the action result. In other words when I debug and examine the values the property for the dropdownlist is nothing. I believe that this is causing the "ModelState.isValid" to return false as well.
Here is my Model:
Public Class Warranty
Public Property state As DropDownList
Private _States As IEnumerable(Of State)
Public Property States As IEnumerable(Of State)
Get
If _States Is Nothing Then
_States = GetStates()
End If
Return _States
End Get
Set(value As IEnumerable(Of State))
_States = value
End Set
End Property
Public Shared Function GetStates() As List(Of State)
Dim l As New List(Of State)
l.Add(New State With {.Value = "none", .Text = "Selected One"})
l.Add(New State With {.Value = "UT", .Text = "Utah"})
l.Add(New State With {.Value = "NV", .Text = "Nevada"})
Return l
End Function
End Class
Here is my State Class:
Public Class State
Public Property Value As String
Public Property Text As String
End Class
Here are my controller methods:
' GET: /Warranty
Function WarrantyRegistration() As ActionResult
ViewData("Message") = "Warranty Registration Form"
Dim _statesList As New SoleWebSite.Models.Warranty
Return View(_statesList)
End Function
'
'POST: /Warranty
<HttpPost()> _
Function WarrantyRegistration(ByVal warranty As SoleWebSite.Models.Warranty) As ActionResult
If ModelState.IsValid Then
war.state = warranty.state.SelectedItem.Value.ToString()
// warranty.state
db.AddTowarranty_registrations(war)
db.SaveChanges()
db.Dispose()
Return RedirectToAction("WarrantyRegistration")
End If
Return View(warranty)
End Function
Here is my view:
<td>@Html.DropDownListFor(Function(m) m.state, New SelectList(Model.States, "Value", "Text"))</td>
I am not sure what I am doing wrong.
I would like to keep everything strongly typed and avoid using viewbag or viewdata if possible.
Any suggestion would be appreciated.
Thanks in advance.
Upvotes: 1
Views: 1114
Reputation: 1038710
The selected value property should be a simple scalar type such as String
or Integer
and not a complex type such as DropDownList
. By the way the DropDownList type is a classic WebForms server side control that has absolutely nothing to do in an ASP.NET MVC application. You should get rid of absolutely any reference to the System.Web.UI.WebControls
namespace in an ASP.NET MVC application. So in your Warranty
use a simple type:
Public Property state As String
and in your POST action you simply read the value:
<HttpPost()> _
Function WarrantyRegistration(ByVal warranty As SoleWebSite.Models.Warranty) As ActionResult
If ModelState.IsValid Then
war.state = warranty.state
// warranty.state
db.AddTowarranty_registrations(war)
db.SaveChanges()
db.Dispose()
Return RedirectToAction("WarrantyRegistration")
End If
Return View(warranty)
End Function
Upvotes: 1