views:

1173

answers:

1

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.

A: 

Huh. It appear that htmlhelper is TOO good. I removed the references to the model and everything works!

i.e.

 <%= Html.DropDownList("Parking", Model.Parking)%>

becomes

 <%= Html.DropDownList("Parking")%>

and we're golden. Is it that ViewData contains something called 'Parking' because I'm referencing it in the model so it squashes the other value...or something...?

The second parameter for the drop down list is the selected value, you have probably strongly typed your view so that the model is your view model, therefore by doing Model.Parking the Parking property is actually your selectlist object.. where as if you do model.Buyer_Profile.Parking that might also work.
Pricey