开发者

How can I display a message in a master page with ViewData?

开发者 https://www.devze.com 2023-01-12 03:04 出处:网络
How can I display a message on the master page. The message is sent by the action. public A开发者_运维技巧ctionResult DisplayMessage()

How can I display a message on the master page. The message is sent by the action.

public A开发者_运维技巧ctionResult DisplayMessage()
{
    ViewData["hello"] = "Hello!";
    return View();
}


This is actually pretty simple. Just add the following in your controller:

ViewData["PassedToMaster"] = "From content page!";

Then in your MasterPage you can just add the following code to look for it and if it is there do something with it:

<% if (ViewData["PassedToMaster"] != null)
   { %>
   <%= ViewData["PassedToMaster"].ToString() %>
<% } %>


In your view, do the following:

<%= html.encode(ViewData("Hello")) %>

If you want to place this data in another area outside of your view within your master page, you will need to define a new content placeholder.

Master Page:

<div id="somewhereOtherThanYourNormalViewArea">
    <asp:ContentPlaceHolder ID="SecondaryContent" runat="server" />
</div>

View:

<asp:Content ID="Content2" ContentPlaceHolderID="SecondaryContent" runat="server">
    <%= html.encode(ViewData("Hello")) %>
</asp:Content>
0

精彩评论

暂无评论...
验证码 换一张
取 消