如何在母版页中按名称呈现部分视图

发布于 2024-10-12 00:38:49 字数 2299 浏览 2 评论 0原文

我正在尝试实现类似 Ruby on Rail 的 ActionDispatch::Flash 将消息传递给下一个页面请求。我想要采取的方法是在会话上下文中存储一对(部分视图名称,模型对象),并在渲染显示闪存消息的请求时将其渲染在母版页中。

在我的 HttpApplication 子类中,我现在有:

public class Global : System.Web.HttpApplication
{
    public class FlashStuff
    {
        private string flashViewName;
        private object model;

        public FlashStuff(string flashViewName, object model)
        {
            this.flashViewName = flashViewName;
            this.model = model;
        }

        public string FlashViewName
        {
            get { return flashViewName; }
        }

        public object Model
        {
            get { return model; }
        }
    }

    internal static void Flash(string flashViewName, object model)
    {
        HttpContext.Current.Session.Add("flash", new FlashStuff(flashViewName, model));
    }

    //...
 }

我还有一个带有添加书签的操作的 BookmarkController:

[HandleError]
public class BookmarkController : Controller
{
    public ActionResult Add()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Add(Bookmark_AddModel model)
    {
        if (ModelState.IsValid)
        {
            //...

            Global.Flash("BookmarkAddedFlash", model);
            return RedirectToAction("Index", "Bookmark");
        }

        //...
    }

    //...
}

在我的母版页中,我现在想要类似的东西:

        <% var session = HttpContext.Current.Session;
           if (session["flash"] != null)
           {
               var flashStuff = (MyApp.Web.Global.FlashStuff)session["flash"]; %>
               <div><!-- render in here --></div>
        <%     session.Remove("flash");
           } %>

渲染部分视图调用 Global#Flash 时通过名称指定的。

我想使用 Flash 消息的视图,而不是在会话上下文中将消息保存为 string,因为我想在消息中包含 URL:

<%-- BookmarkAddedFlash.ascx --%>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyApp.Web.Models.Bookmark_AddModel>" %>
<%: Html.DisplayTextFor(m => m.Url) %> was successfully bookmarked.

并且我只想在以下情况下呈现视图 :随后提出请求;即,一个请求设置闪现消息,随后的请求呈现它。

我该怎么做?

I am trying to implement something like Ruby on Rail's ActionDispatch::Flash to pass messages to the next page request. The approach that I want to take is to store a pair of (partial view name, model object) in the session context and render it in the master page when rendering the request to display the flash message.

Inside my HttpApplication subclass I now have:

public class Global : System.Web.HttpApplication
{
    public class FlashStuff
    {
        private string flashViewName;
        private object model;

        public FlashStuff(string flashViewName, object model)
        {
            this.flashViewName = flashViewName;
            this.model = model;
        }

        public string FlashViewName
        {
            get { return flashViewName; }
        }

        public object Model
        {
            get { return model; }
        }
    }

    internal static void Flash(string flashViewName, object model)
    {
        HttpContext.Current.Session.Add("flash", new FlashStuff(flashViewName, model));
    }

    //...
 }

I also have a BookmarkController with an action to add a bookmark:

[HandleError]
public class BookmarkController : Controller
{
    public ActionResult Add()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Add(Bookmark_AddModel model)
    {
        if (ModelState.IsValid)
        {
            //...

            Global.Flash("BookmarkAddedFlash", model);
            return RedirectToAction("Index", "Bookmark");
        }

        //...
    }

    //...
}

In my master page I now want something like:

        <% var session = HttpContext.Current.Session;
           if (session["flash"] != null)
           {
               var flashStuff = (MyApp.Web.Global.FlashStuff)session["flash"]; %>
               <div><!-- render in here --></div>
        <%     session.Remove("flash");
           } %>

to render the partial view that was specified by name when Global#Flash was called.

I want to use a view for the flash message instead of saving the message as a string in the session context because I want to include the URL in the message:

<%-- BookmarkAddedFlash.ascx --%>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyApp.Web.Models.Bookmark_AddModel>" %>
<%: Html.DisplayTextFor(m => m.Url) %> was successfully bookmarked.

And I only want to render the view when the subsequent request is made; i.e. one request sets the flash message and the subsequent request renders it.

How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感受沵的脚步 2024-10-19 00:38:49

ASP.NET MVC 中 RoR 的 Flash 对象的等效项称为 临时数据

public ActionResult Action1()
{
    TempData["message"] = "some message";
    return RedirectToAction("action2");
}

public ActionResult Action2()
{
    var message = TempData["message"] as string;
    return View();
}

或者直接在 Action2 视图中使用它:

<div><%: TempData["message"] %></div>

TempData 在内部使用 Session 对象来存储数据,但在一次重定向后它会自动失效。所以你想要实现的已经存在了。

The equivalent of RoR's Flash object in ASP.NET MVC is called TempData.

public ActionResult Action1()
{
    TempData["message"] = "some message";
    return RedirectToAction("action2");
}

public ActionResult Action2()
{
    var message = TempData["message"] as string;
    return View();
}

or use it directly in the Action2 view:

<div><%: TempData["message"] %></div>

Internally TempData uses the Session object to store data but it is automatically invalidated after one redirect. So what you are trying to implement already exists.

青萝楚歌 2024-10-19 00:38:49

我不确定我是否完全理解。我最近实现了一个“咆哮”基础设施,我使用了一个 ASP.NET MVC 项目。在某些方面听起来很相似。以下是该实现的链接:

http:// blog.bobcravens.com/2010/08/making-the-web-growl-using-jquery/

希望这会有所帮助。

鲍勃

I'm not certain I fully understand. I recently implemented a 'growl' infrastructure that I used an an asp.net mvc project. It sounds similar in some respects. Here is a link to that implementation:

http://blog.bobcravens.com/2010/08/making-the-web-growl-using-jquery/

Hope this helps.

Bob

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文