I'm building my first MVC application, having followed the 'NerdDinner' tutorial through. In creating a dropdownlist from a SelectList in the same way, however, I'm running into an issue.
For some reason, when I bring up the 'Edit' view, the dropdownlist isn't showing the correct selection, even though the data is set otherwise in the database and the 'Details' view shows the correct value. Every one is just coming up with the first value in the list.
I've been through the NerdDinner code piece by piece and can't for the life of me see any difference, yet that application will correctly show the dropdown with the current value when editing, and mine doesn't.
Anyone have a suggestion for where to go from here? I can post code snippets if someone asks for something specific.
Update:
Within a fieldset:
<p>
<label for="Parking">Parking Arrangement:</label>
<%= Html.DropDownList("Parking", Model.Parking)%>
<%= Html.ValidationMessage("Parking", "*") %>
</p>
The Edit action:
//
// GET: /Buyer/Edit/2
public ActionResult Edit(int id)
{
Buyer_Profile buyer_profile = buyerRepository.GetBuyerProfileByID(id);
if (buyer_profile == null)
return View("NotFound");
else if (!buyer_profile.IsOwnedBy(User.Identity.Name, id))
return RedirectToAction("Index", "Home");
else
return View(new BuyerFormViewModel(buyer_profile));
}
In the same way they construct it for the NerdDinner example, I created a '...FormViewModel':
public class BuyerFormViewModel
{
// Properties
public Buyer_Profile Buyer_Profile { get; private set; }
public SelectList Parking { get; private set; }
// Constructor
public BuyerFormViewModel(Buyer_Profile buyer_profile)
{
Buyer_Profile = buyer_profile;
Parking = new SelectList(BuyerProfileOptions.Parking, Buyer_Profile.Parking);
}
}
And the generated HTML when clicking on 'edit' when a value is already shown in the details view and stored in the d/b:
<p>
<label for="Parking">Parking Arrangement:</label>
<select id="Parking" name="Parking"><option>No Preference</option>
<option>On Street</option>
<option>Assigned Street</option>
<option>Open Garage</option>
<option>Covered Garage</option>
</select>
</p>
The text fields in the same form have their values populated correctly. It's just all the dropdowns which don't!
Many thanks for your attention.