views:

346

answers:

1

I have the following on my edit screen:

<label for="campaign.CandidateID">Candidate:</label>
<%= Html.DropDownList("Campaign.CandidateID", Model.Candidates, "Choose...")%>
<%= Html.ValidationMessage("CandidateID", "*") %>

and in my controller:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
    Campaign campaign = repos.GetCampaign(id);
    try
    {
     UpdateModel(campaign);
     repos.Save();
     return RedirectToAction("Index", "Admin");
    }
    catch
    {
     return View(new CampaignDTO(campaign));
    }

When I edit the record and change text fields, everything works perfectly, but when I change an item connected to a dropdownlist, the change does not get updated in the campaign object. I have checked this.ValueProvider["Campaign.CandidateID"] and the data is in there!

By the way, during the display of the edit screen, it is selecting the correct Candidate in the select list.

Could the problem come from the fact that ValueProvider is providing a string whereas CandidateID in my class is an int?

I'm stumped.

+1  A: 

Are you prefixing the text field's name with "Campaign" as well (i.e. Html.TextBox("Campaign.Name")? When you call UpdateModel without specifying a prefix, any data in the ValueProvider dictionary that contains a prefix (i.e. "Campaign.CandidateID") won't be eligible for binding to the model object. Which is why you see the "Campaign.CandidateID" entry in the ValueProvider dictionary (because it was successfully posted in the request), but it isn't being bound to your Campaign object.

LostInTangent
No, my text boxes did not have "Campaign" prefixed. So I prefixed them all with "Campaign" and changed the call to UpdateModel to UpdateModel(campaign, "Campaign") and it now works! THANK YOU. I think what threw me is that the fields were showing the proper data, it was only the UpdateModel that was having an issue.
Wavel