2

I'm having a strange issue with RedirectToAction in MVC 3.0.

Here is the code of my sample ViewModel

public class EventViewModel
{
  [Required(ErrorMessageResourceType = typeof(Resources.Validations), ErrorMessageResourceName = "Required")]
  public DateTime CreationDate { get; set; }

  [Required(ErrorMessageResourceType = typeof(Resources.Validations), ErrorMessageResourceName = "Required")]
  [AllowHtml] //here is my apparent problem
  public string Description { get; set; }

  [Required(ErrorMessageResourceType = typeof(Resources.Validations), ErrorMessageResourceName = "Required")]
  [Range(0, 5, ErrorMessageResourceType = typeof(Resources.Validations), ErrorMessageResourceName = "RangeValue")]
  public int Rating { get; set; }
  [Required(ErrorMessageResourceType = typeof(Resources.Validations), ErrorMessageResourceName = "Required")]
  public string Title{ get; set; }

  ...other properties...

}

Here is the two methods of my controller

public ActionResult Edit(int id)
{
  var entity = eventsRepository.Get(id);
  if (entity == null)
    return RedirectToAction("Index");
  var eventVM = new EventViewModel();
  eventVM.Description = entity.Description;

  ... set the other properties ...

  return View(eventVM);
}

[HttpPost]
public ActionResult Edit(int id, EventViewModel model)
{
  if (ModelState.IsValid)
  {
    try
    {
      var entity = eventsRepository.Get(id);
      if (entity == null)
        return RedirectToAction("Index");
      entity.CreationDate = model.CreationDate;
      entity.Description = model.Description;

      ... set the other properties ...

      eventsRepository.Save(entity);
      return RedirectToAction("Index");
    }
    catch (Exception e)
    {
      ModelState.AddModelError("", "An error occured bla bla bla");
    }
  }

  return View(model);
}

My problem is, if I remove the AllowHtmlAttribute and insert plain text in the description field, all is ok and I get my redirect after save, but if I put the AllowHtmlAttribute on the field description and insert some Html text, after save instead of the redirect I get a blank page with only this text:

Object moved to here. 

If I click on "here", I'm redirected to the right url.

Am I missing something obvious?

Iridio
  • 9,213
  • 4
  • 49
  • 71

3 Answers3

3

read this forum post. the short of it is that you will receive that error message when the http headers have been modified before the redirect is processed. i'm not sure entirely how the [allowhtml] attribute could cause that, but at least its a jumping point i suppose.

nathan gonzalez
  • 11,817
  • 4
  • 41
  • 57
  • I know about the header problem, and looking with Fiddler I do not see any differences. Guess I have to create a simple project and start from scratch to try to found the real problem. Me too I'm dubious about the allowhtml, but if I remove it everything works, so the problem should lie in the way html value is processed. – Iridio May 27 '11 at 14:05
  • @Iridio, that would be the route i'd go as well. good luck solving it, and when you do figure it out be sure to post the answer back here. – nathan gonzalez May 27 '11 at 14:16
  • I made a project from scratch with a fake repository and all works as expected. So I start supposing there is something else that go nuts with the [AllowHtml] attribute. Maybe the save of my repository. (unlikely, but is the only difference I have) when I have little more time I will rewrite my code again and try to find out the real cause and get back here. – Iridio May 28 '11 at 08:24
2

Its actually cause by a httpModule interfering with mvc. I had same issue and fixed it by removing the following from the main web.config.

<add type="DevExpress.Web.ASPxClasses.ASPxHttpHandlerModule, DevExpress.Web.v11.1, Version=11.1.10.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" name="ASPxHttpHandlerModule" />
Matt
  • 36
  • 2
  • Thank you that solved the problem. I did not notice that in the new sample project I haven't included the call of the devexpress in the httpmodule. – Iridio Feb 08 '12 at 09:05
0

RedirectToAction will send a new HTTP Response to the browser. I don't understand what ASP.Net is doing behind the scenes, but apparently it's creating a page that has a link to the Action you specified in the RedirectToAction call.

I'm not entirely clear on what entity is in Edit(int id, EventViewModel model), but it seems like you can use it to get the EventViewModel you need for the Edit view.

In

Edit(int id, EventViewModel model)

After

... set the other properties ...

eventsRepository.Save(entity);

Add

var eventVM = new EventViewModel();
eventVM.Description = entity.Description;

... set the other properties ...

return View(eventVM);
Steve Mallory
  • 4,245
  • 1
  • 28
  • 31
  • I made a mistake in my sample code. after the save I want to redirect to index and not to edit again. Anyway in both cases I get the same error. Instead if I return as you suggest View(eventVM) all works fine, but is not what I need :( – Iridio May 28 '11 at 08:14