简体   繁体   English

如何强制Mono MVC3应用程序在当前响应中显示TempData

[英]How to force Mono MVC3 application to show TempData in current response

If ASP.NET MVC2 code below shows message "Test" in Create controller response. 如果下面的ASP.NET MVC2代码在“创建控制器响应”中显示消息“测试”。

In Mono message does not appear apper in Create view. 在“单声道”中,消息不会出现在“创建”视图中。

Message appears in next response invoked after create. 消息出现在创建后调用的下一个响应中。

How to dorce Mono to show TempData value in same request like in ASP.NET ? 如何像在ASP.NET中一样在同一个请求中将Mono放置为显示TempData值?

[HttpPost]
public RedirectToRouteResult Create()
{
 TempData["Message"] = "Test";
 return RedirectToAction("Index");
}


public ActionResult Index() {
  return View();
  }

Site.Master: Site.Master:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <script src="<%= Url.Content("~/Scripts/jquery/jquery-1.7.1.js")%>" type="text/javascript"></script>

 <% if (TempData["Message"]!=null) {
        %>
    $(function() {
        setTimeout( function() {
           showMessage ( '<%= TempData["Message"] as string %>');
           }, 300 );
      });
        <% } %>
    </script>
</head>

I would recommend you consuming the TempData value inside the controller action and not inside the view: 我建议您在控制器操作而不是视图内部使用TempData值:

[HttpPost]
public ActionResult Create()
{
    TempData["Message"] = "Test";
    return RedirectToAction("Index");
}

public ActionResult Index() 
{
    ViewData["Message"] = TempData["Message"];
    return View();
}

and inside the view: 在视图内部:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <script src="<%= Url.Content("~/Scripts/jquery/jquery-1.7.1.js")%>" type="text/javascript"></script>

    <% if (ViewData["Message"] != null) { %>
        $(function() {
            window.setTimeout(function() {
                showMessage (<%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewData["Message"]) %>);
            }, 300);
        });
    <% } %>
    </script>
</head>

By the way instead of using ViewData , I would recommend you using a view model and have your view strongly typed to this view model. 顺便说一句,而不是使用ViewData ,我建议您使用视图模型,并强烈地将您的视图键入此视图模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM