Script to make a div appear

Asked

Viewed 321 times

2

I have a user registration in my application, when I click register wanted to show a message. This whole treatment I’m already doing and playing the message in my Viewbag.Message. How do I show this DIV by clicking the sign up button ?

  • if you show the div, the message will already appear automatically ?

  • Yes, because my Viewbag.Message is inside the div.

2 answers

4

After clicking register, you have some ways to show the div:

1st -> $("#idDiv").show();

2nd -> document.getElementById('idDiv').style.display = 'block';

3rd -> $("#idDiv").css("display", "block");

I believe the first one works.

3

You can check whether the ViewBag.Message is not empty, so display a message.

Below is an example using jQueryUI:

Controller

using System;
using System.Web.Mvc;

namespace SampleApp
{
    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Index(string mensagem)
        {               
            ViewBag.Messagem = mensagem;
            return View();
        }
    }
}

View

@{
    Layout = null;
    var mensagem = string.Empty;
    if (ViewBag.Messagem != null) {
        mensagem = ViewBag.Messagem.ToString();
    }   
}

<!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></title>
        <link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.11.4/themes/flick/jquery-ui.css" />
        <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.js"></script>
        <script type="text/javascript" src="https://code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
    </head>

    <body>
        @using (Html.BeginForm())
        {
            <input type="hidden" name="mensagem" value="Hello World!"></input>
            <input type="submit" value="Enviar" />
        }
        <div id="mensagem">
        </div>
        <!-- Este script deveria está em um arquivo e não inline -->
        <script type="text/javascript">
            var mensagem = document.getElementById("mensagem");
            $(function () {
                $(mensagem).dialog({
                    autoOpen: false,
                    modal: true
                });             
            });
            var exibirMensagem = function (msg) {
                mensagem.innerHTML = msg;
                $(mensagem).dialog("open");
            }
        </script>
        @if (!String.IsNullOrWhiteSpace(mensagem)) {
        <script type="text/javascript">
            $(function () {
                exibirMensagem("@ViewBag.Messagem");
            });
        </script>
        }
    </body>
</html>

Note that the method exibirMensagem is only called if ViewBag.Messagem hold a value.

Follows a fiddle: Dotnetfiddle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.