MVC RedirectToAction的奇怪行为

时间:2012-02-17 15:05:36

标签: asp.net-mvc-3 redirecttoaction

我有以下UserController方法:

public ActionResult DeleteThread(int RootMessageID)
{
    _repository.DeleteMessageThread(RootMessageID);
    return RedirectToAction("ActionMessageSuccess", new { txt = "Message was sent successfully" });
}
    public ActionResult ActionMessageSuccess(string txt)
    {
        return View(txt);
    }

有cshtml页面(在Views \ User中):

@model System.String
@{
    ViewBag.Title = "SendMessageSuccess";
    Layout = "~/Views/Shared/_LayoutUser.cshtml";
}

<h2>@Model</h2>

但我得到了:

The view 'Message was sent successfully' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/User/Message was sent successfully.aspx
~/Views/User/Message was sent successfully.ascx
~/Views/Shared/Message was sent successfully.aspx
~/Views/Shared/Message was sent successfully.ascx
~/Views/User/Message was sent successfully.cshtml
~/Views/User/Message was sent successfully.vbhtml
~/Views/Shared/Message was sent successfully.cshtml
~/Views/Shared/Message was sent successfully.vbhtml

调用DeleteThread后。为什么呢?

1 个答案:

答案 0 :(得分:1)

redirectToAction没有问题。

您正在呼叫RedirectToAction("ActionMessageSuccess", new { txt = "Message was sent successfully" });

因此,它重定向到具有 txt 值的ActionMessageSuccess动作。

在ActionMessageSuccess中,您将返回名称为txt的视图,即“消息已成功发送”,但系统无法提交名为“消息已成功发送”的视图。