问题描述
|
我在MVC 3.0中遇到RedirectToAction的奇怪问题。
这是我的示例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...
}
这是我控制器的两种方法
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);
}
我的问题是,如果我删除AllowHtmlAttribute
并在描述字段中插入纯文本,一切正常,并且保存后得到重定向,但是如果我将description2ѭ放在字段说明中并在保存后插入一些HTML文本,而不是重定向我得到一个只有以下文本的空白页:
Object moved to here.
如果单击“此处”,则会重定向到正确的URL。
我是否缺少明显的东西?
解决方法
它实际上是由httpModule干扰mvc引起的。我遇到了同样的问题,并通过从主web.config中删除以下内容来解决了该问题。
<add type=\"DevExpress.Web.ASPxClasses.ASPxHttpHandlerModule,DevExpress.Web.v11.1,Version=11.1.10.0,Culture=neutral,PublicKeyToken=b88d1754d700e49a\" name=\"ASPxHttpHandlerModule\" />
,阅读此论坛帖子。简而言之,当在处理重定向之前修改了HTTP标头时,您将收到该错误消息。我不确定[allowhtml]属性是怎么造成的,但我想至少是一个跳跃点。
,RedirectToAction将向浏览器发送新的HTTP响应。我不了解ASP.Net在幕后做什么,但是很显然,它正在创建一个页面,该页面具有指向您在RedirectToAction调用中指定的Action的链接。
我对clear6ѭ中的实体尚不完全清楚,但似乎可以使用它来获取“编辑”视图所需的EventViewModel
。
在
Edit(int id,EventViewModel model)
后
... set the other properties ...
eventsRepository.Save(entity);
加
var eventVM = new EventViewModel();
eventVM.Description = entity.Description;
... set the other properties ...
return View(eventVM);