My jQuery code hides a ddl under certain circumstances. When this is the case, after submitting the form, using the UpdateModel doesn’t seem to work consistently. My code in the controller:
// POST: /IllnessDetail/Edit
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(IllnessDetail ill)
{
IllnessDetailFormViewModel mfv = new IllnessDetailFormViewModel(ill);
if (ModelState.IsValid)
{
try
{
IllnessDetail sick = idr.GetLatestIllnessDetailByUsername(User.Identity.Name);
UpdateModel(sick);
idr.Save();
return RedirectToAction("Current", "IllnessDetail");
}
catch
{
ModelState.AddRuleViolations(mfv.IllnessDetail.GetRuleViolations());
}
}
return View(new IllnessDetailFormViewModel(ill));
}
I have only just started with MVC, and working under a deadline, so am still hazy as to how UpdateModel works. Debugging seems to reveal that the correct value is passed in to the action method:
public ActionResult Edit(IllnessDetail ill)
And the correct value is put into sick in the following line:
IllnessDetailFormViewModel mfv = new IllnessDetailFormViewModel(ill);
However, when all is said, done and returned to the client, what displays is the value of:
sick.IdInfectiousAgent
instead of the value of:
ill.IdInfectiousAgent
The only reason I can think of is that the ddlInfectiousAgent has been hidden by jQuery. Or am I barking up the wrong lamp post?
Andrew
Just realised I hadn’t updated this. The cause of the whole issue was not related to jQuery at all, as an error was happening in the server code that was being “swallowed” by the AJAX response.
I finally detected the error by opening the page in FireFox and using FireBug to see if any errors happened. This may seem obvious to some, but I was new to AJAX. But then I downloaded FireBug and saw that it was good…
The jQuery was actually just (correctly) telling me that no results had been returned by the call. It didn’t cross my mind that this might be because an error had been returned instead.
The error in question was that I was using an HttpGet in an app upgraded to MVC 2, so got snagged by the new JsonRequestBehavior parameter, as in:
This parameter didn’t exist in MVC 1, and I was unaware of its adition to MVC 2. RTFM, as they say.