【发布时间】:2012-04-05 15:53:36
【问题描述】:
我在 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 并在描述字段中插入纯文本,一切正常,保存后我会获得重定向,但如果我将 AllowHtmlAttribute 放在字段描述中并插入一些 Html文本,保存而不是重定向后,我得到一个只有以下文本的空白页:
Object moved to here.
如果我点击“这里”,我会被重定向到正确的网址。
我是否遗漏了一些明显的东西?
【问题讨论】:
标签: c# asp.net-mvc-3