Reputation: 33
Been stuck on this for 2 days...hoping you guys can help.
Using VB.Net with MVC4 framework.
I have an Html.DropDownListFor in my View which looks like this:
@Html.DropDownListFor(Function(model) model.Title, New SelectList(Model.Titles, "Key", "Value"), "-select-", New With {.id = "titles", .class = "form-control"})
Model.Titles is a Dictionary(Of String, String) which is such a short list that I'd rather pull it directly from a function instead of the database. Got the idea to use Dictionary list from here:
My function looks like this, which gets pulled into my Model as Model.Titles
Public Function getAgcRepTitles() As Dictionary(Of String, String)
Dim list As New Dictionary(Of String, String)
list.Add("A/D/SGT", "A/D/SGT")
list.Add("A/SGT", "A/SGT")
list.Add("CPL", "CPL")
list.Add("DET", "DET")
Return list
End Function
Everything works great, it displays the list and saves the selected value to the database. In debug mode, when I explore the DropDownListFor in the View, I can see that Model.Title has retrieve the correct value from the database (eg. "DET"), and I can see the Model.Titles contains the above list from my function. But as hard as I try, I can't get the DropDownListFor to display the Model.Title from the list.
The Select element within the HTML doesn't have a selected Option, but otherwise looks good:
<select name="Title" class="form-control">
<option value="">-select-</option>
<option value="A/D/SGT">A/D/SGT</option>
<option value="A/SGT">A/SGT</option>
<option value="CPL">CPL</option>
<option value="DET">DET</option>
</select>
I've tried adding the Model.Title value to the SelectedValue of the SelectList in the View. I've tried using a list of SelectListItems instead of Dictionary and sending it to the View via Viewbag.myTitles. I've tried using integers for the Key values (0,1,2,3...). Nothing works.
I should also add that I have another DropDownListFor in the same View that works perfectly. When you compare them....this one pulls its list from the database as SelectListItems into a SelectList (which uses Value and Text, instead of Key and Value respectively), where the Value is an integer starting at zero and the Text is string.
Really banging my head with this one.
EDIT: More info. When I submit the form, the modified data is successfully uploaded to the database, and the Controller sends back the model data to the View. Okay....in THIS View the DropDownListFor works!!! But then I click refresh, and I lose my selected value from the DropDownListFor (even though my model data is IDENTICAL).
Here is my Controller:
<AllowAnonymous()> _
Function Update() As ActionResult
If WebSecurity.IsAuthenticated Then
Dim user = db.AgcRepProfile.Where(Function(AgcRep) AgcRep.Email = WebSecurity.CurrentUserName).ToList
If user.Count = 1 Then
Dim getAgencies = db.AgencyProfile.Where(Function(Agency) Agency.Active = True).OrderBy(Function(Agency) Agency.Name).ToList
ViewBag.myAgencies = New SelectList(getAgencies, "AgcId", "Name")
user(0).Titles = New myLists().getAgcRepTitles
ViewData("success") = ""
Return View(user(0))
End If
End If
Return RedirectToAction("index", "home")
End Function
<HttpPost()> _
<ValidateAntiForgeryToken()> _
Function Update(agcrep As AgcRep) As ActionResult
If ModelState.IsValid And WebSecurity.IsAuthenticated Then
db.Entry(agcrep).State = EntityState.Modified
db.SaveChanges()
Dim agencies = db.AgencyProfile.Where(Function(Agency) Agency.Active = True).OrderBy(Function(Agency) Agency.Name).ToList
ViewBag.myAgencies = New SelectList(agencies, "AgcId", "Name", agcrep.AgcId.ToString)
agcrep.Titles = New myLists().getAgcRepTitles
ViewData("success") = "changes have been saved"
Else
ViewData("success") = "Update failed. Please try again."
End If
Return View(agcrep)
End Function
Upvotes: 0
Views: 662
Reputation: 33
Got it! Sweet Jebus that was painful.
Turns out you can't have a ViewData or ViewBag containing a variable with the same name as your Model variables.
As soon as I removed the Viewbag("title") from the top of the View, my Model.Title worked perfectly.
Upvotes: 1