Using MVC 1.0 I have the following custom form view model
public class NoteFormViewModel
{
// Properties
public Note Note { get; private set; }
public SelectList PrivacyOptions { get; private set; }
// Constructor
public NoteFormViewModel(Note note)
{
SharePermissionsRepository sharePermissionsRepository = new SharePermissionsRepository();
var sharePermissions = sharePermissionsRepository.FindAllSharePermissions();
Note = note;
PrivacyOptions = new SelectList(sharePermissions, "id", "share_permission", note.share_permission_id);
}
}
and two DB tables. Notes and SharedPermissions. Notes.shared_permission_id (integer) is a foreign key to SharedPermissions.id (integer, primary key, identity)
The edit view is bound to NoteFormViewModel and the dropdown list is bound as follows.
<%= Html.DropDownList("share_permission_id", Model.PrivacyOptions)%>
Then on submit of the form i do this.
[AcceptVerbs(HttpVerbs.Post)]
[ValidateInput(false)]
public ActionResult Edit(int id, FormCollection formValues)
{
Note note = notesRepository.GetNote(id);
try
{
UpdateModel(note);
notesRepository.Save();
return RedirectToAction("Details", new { id = note.id });
}
catch
{
ModelState.AddModelErrors(note.GetRuleViolations());
return View(new NoteFormViewModel(note));
}
}
But the note.shared_permission_id
is not updated, even though the forms collection contains a key of "shared_permission_id" and the correct updated value.
I do not get any exceptions.
If however i explicity set the note.share_permission_id
like this.
UpdateModel(note);
note.share_permission_id = System.Convert.ToInt32(Request.Form["share_permission_id"]);
notesRepository.Save();
return RedirectToAction("Details", new { id = note.id });
Then the share_permission_id
is updated correctly.
I've trawled some of the other posts on here and i can see how they are related to this but, not quite close enough for me to understand exactly what is going on.
What's going on? Is this a bug in the UpdateModel method or is it something else completely?