Presenting message in a view via the C # MVC Controller

6

Looking for a solution I found a form on the link Controller message for the view

But my problem is that nothing is displayed. I do not know if it's because I do not have an action in View but the message does not appear.

The structure is set up in this way (the base structure of the previous link was maintained and mounted almost the same):

Controller

public ActionResult Relatorios()
        {
            ViewBag.Message = "Mensagem";
            ViewBag.TipoUser = 1;

            this.FlashInfo(this, "Mensagem de Informação.");
            this.FlashWarning("Mensagem de Aviso.");
            this.FlashError("Mensagem de Erro.");
        }

Inside the View I only have the following code:

@model dynamic

@{
    ViewBag.Title = "Relatórios";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}

Report Management

Within _Layout put within the following segment

<div class="container-fluid" style="margin-left: 10px; margin-right:5px; margin-bottom: 10px; margin-top: 10px">
    @Flash.FlashMessage(TempData)
    <div id="flash">
    </div>  
    @RenderBody()
    <hr/>
    <footer style="margin-top: 10px">
       <p>&copy; @DateTime.Now.Year - Empresa</p>
    </footer>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/bundles/site")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/inputmask")
@Scripts.Render("~/bundles/maskedinput")
@RenderSection("scripts", required: false)
</body>
</html>

Can anyone help me?

    
asked by anonymous 06.01.2016 / 14:39

1 answer

3

There are a few ways. I've implemented a very simple solution and you can access it through this .net fiddle

For limitations of the tool I did not use inheritance, which I highly recommend the use.

Feel free to extend this simple solution.

I hope I have helped.

  

Update: I'll put the code here to avoid future issues like dotnetfiddle's link break but for preview / execution   online will keep the .net fiddle

MyDialog.cs     

namespace MyDialogMvcApp
{
    public class MyDialog
    {
        public enum DialogType : short {
            Info = 0,
            Success = 1,
            Warning = 2,
            Error = 3
        }

        public string Title { get; set; }
        public string Content { get; set; }
        public DialogType @Type { get; set; }

        public override string ToString() => $"{{ \"title\": \"{Title}\", \"content\": \"{Content}\", \"type\": \"{@Type.ToString().ToLower()}\" }}";
    }
}

BaseController.cs     

using System;
using System.Web.Mvc;

namespace MyDialogMvcApp
{
    public abstract class BaseController : Controller
    {
        public const string SystemMessage = "MY_DIALOG";

        protected void ShowMessage(string htmlContent, string htmlTitle = "Mensagem do Sistema", MyDialog.DialogType type = MyDialog.DialogType.Info)
        {
            this.ShowMessage(new MyDialog{ Title = htmlTitle, Content = htmlContent, @Type = type});
        }

        protected void ShowMessage(MyDialog dialog)
        {
            this.TempData[SystemMessage] = dialog.ToString(); 
        }
    }
}

HomeController.cs     

using System;
using System.Web.Mvc;

namespace MyDialogMvcApp
{
    public class HomeController : BaseController
    {
        [HttpGet]
        public ActionResult Index()
        {
            this.ShowMessage("Uma mensagem de teste =)");
            return View();
        }
    }
}

Index.cshtml or _Layout.cshtml (with @RenderBody ())     

@{  Layout = null; }

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Bootstrap 101 Template</title>

        <!-- CSS Includes -->
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
        <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css">

    </head>

    <body>
        <div class="container">
            <div class="col-md-6 col-md-offset-3">
                <h1>E ai, cara!?</h1>
                <p>Espero que você tenha recebido um alerta quando a página carregou. ;)</p>
                <p>Caso não tenha notado, <a href="javascript:void(0);" id="aShowMessage">clique aqui</a> para que a mensagem seja exibida novamente.
            </div>
        </div>

        <!-- JS includes -->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>


        <script type="text/javascript">
            function showMessage(message){
                toastr[message.type](message.content, message.title);
            }

            $(function(){
                var systemMessage = @Html.Raw(TempData[MyDialogMvcApp.BaseController.SystemMessage]);               

                showMessage(systemMessage);

                $('#aShowMessage').click(function(){ showMessage(systemMessage) });
            });
        </script>
    </body>
</html>
    
27.01.2016 / 05:18