I have a user registry in my application, when I click on register I wanted to display 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 register button?
I have a user registry in my application, when I click on register I wanted to display 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 register button?
After clicking on 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.
You can check that ViewBag.Message
is not empty, then 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><scripttype="text/javascript" src="https://code.jquery.com/ui/1.11.3/jquery-ui.js"></script></head><body>@using(Html.BeginForm()){<inputtype="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 exibirMensagem
method is only called if ViewBag.Messagem
has a value.
Here's a fiddle: DotNetFiddle