MVC RedirectToAction从一个控制器到另一个 - 视图不显示问题
问题描述:
我有一个控制器在其中进行一些检查。MVC RedirectToAction从一个控制器到另一个 - 视图不显示问题
如果由于某种原因或发生异常,我想在另一个控制器的另一个视图中显示错误消息。
这是我的异常处理程序的外观
catch (Exception ex)
{
string infoMsg = "Delete the user and recreate is with an appropriate username of both a first and last name which is required for a doctor";
string errorMsg = ex.Message;
// ErrorController ec = new ErrorController();
return this.RedirectToAction("Index", "Error", new { errorMessage = errorMsg, infoMessage = infoMsg });
}
这是对的ActionResult接收呼叫。
public ActionResult Index(string errorMessage, string infoMessage)
{
var db = new DB();
OccuredError occuredError = new OccuredError();
occuredError.ErrorMsg = errorMessage;
occuredError.InfoMsg = infoMessage;
DateTime dateTime = DateTime.Now;
occuredError.TimeOfError = dateTime;
db.OccuredErrors.InsertOnSubmit(occuredError);
db.SubmitChanges();
return View(occuredError);
}
这是一个是强类型的错误索引视图
<h2>Index</h2>
<table>
<tr>
<th></th>
<th>
Error Message
</th>
<th>
Info Message
</th>
<th>
Time Of Error
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.ActionLink("Details", "Details", new { id=item.ErrorId })%> |
</td>
<td>
<%= Html.Encode(item.ErrorId) %>
</td>
<td>
<%= Html.Encode(item.ErrorMsg) %>
</td>
<td>
<%= Html.Encode(item.InfoMsg) %>
</td>
<td>
<%= Html.Encode(String.Format("{0:g}", item.TimeOfError)) %>
</td>
</tr>
<% } %>
</table>
我的问题是,用户没有从最初的视图(一个重定向是有例外)到错误索引视图。调试时,我可以看到它通过错误索引ActionResult运行正常,数据放入数据库。显示索引视图的东西只会导致我认为的问题。
任何建议,我在做什么错在这里。
答
作为一个快速测试,更改: return View(occuredError);
到: return View("Error", occuredError);
其中 “错误” 是视图的名称。
马特
没有意识到这个日期,对不起! – 2011-03-01 16:25:33