How to display a JavaScript message in an ASP.NET MVC view?

1

I have this code:

@if (!string.IsNullOrWhiteSpace (Model.ErrorMessage))
{
     <Script>
         $ ("#ModalError').modal.('show.');
     </Script>

     Response.Write("<script>alert('Olá');</script>");

     HttpContext.Current.Response.Write("<script>alert('Olá');</script>");
}

Where I check if Model.Error message is different from empty, so I wanted to give an alert to the user displaying the error message, but none of the way it is in the middle of this condition is working.

I've tried it too:

@if (!String.IsNullOrEmpty(ViewData["erro"] as string))
{
    <script>alert(@ViewData["erro"]);</script>

}

This is a part of the view.

My controller looks like this:

public ActionResult Login(LoginViewModel model, SignInMessage message)
        {
            if (!String.IsNullOrEmpty(model.ErrorMessage))
                ViewData["erro"] = !String.IsNullOrEmpty(model.ErrorMessage) ? model.ErrorMessage : null;

            return this.View(model);
        }

Does anyone know how to solve it?

    
asked by anonymous 15.09.2016 / 20:20

4 answers

1

Friend this error that you have to set an event to this action I think the correct one would be in the ready of the page!

Well, I guess that's what you need to be sure to see!

I hope I have helped .. Good Luck!

$(document).ready(function(){
    $('#modalError').modal();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
     <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script><!DOCTYPEhtml><html><head><title>exemple</title><metacharset="utf-8">
</head>
<body>

<div id="modalError" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                <p>Message.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

</body>
</html>
    
16.09.2016 / 01:58
0

You've tried to use

@Html.Raw("<script>............</script>");

?

    
15.09.2016 / 21:19
0

With ViewBag :

In the View, enter:

@if (!string.IsNullOrWhiteSpace(ViewBag.Erro))
{
    <script>alert('Olá');</script>
}

No Controller:

public ActionResult Login(LoginViewModel model, SignInMessage message)
{
  ViewBag.Erro = model.ErrorMessage == null ? "" : model.ErrorMessage;
  return View();
}

With Model :

In the View, enter:

@model NamespaceCompletodaModel.LoginViewModel 

@if (!string.IsNullOrWhiteSpace(Model.ErrorMessage))
{
    <script>alert('Olá');</script>
}

No Controller:

public ActionResult Login(LoginViewModel model, SignInMessage message)
{
  model.ErrorMessage = model.ErrorMessage == null ? "" : model.ErrorMessage;
  return View(model);
}

Can not use Response through View.

    
15.09.2016 / 20:31
0

I did so and it worked.

Controller:

public class MensagemController : Controller
{
    // GET: Mensagem
    public ActionResult Index()
    {
        ViewData["erro"] = "Mensagem de Erro";

        return View();
    }
}

View:

@Html.Raw("<script>alert('" + ViewData["erro"] as string + "');</script>")
    
15.09.2016 / 21:53