asp.net mvc - MVC - Display messages from the controller -
within mvc application have several situations where, depending on business rules of function, messages should displayed user.
we using jquery "toastr" library display messages user. development follows:
1) configuring message settings (alert message.cshtml)
@helper mostrarmensagensalerta(alerta alerta) { if (alerta != null) { <script> $(document).ready(function () { window.toastr.options.closebutton = '@alerta.mostrarbotaofechar'; window.toastr.options.newestontop = '@alerta.mostrarnotopo'; @foreach (aperam.biblioteca.util.base.entidades.mensagemalerta mensagem in alerta.mensagensalerta) { string tipoalerta = mensagem.tipoalerta.tostring("f").tolower(); @: var opcoes = { /* adicione atributos específidos dos alertas toastr aqui */ }; @:opcoes.closebutton = true; @:opcoes.progressbar = true; @:opcoes.positionclass = "toast-top-right"; if (mensagem.manteralerta) { @:opcoes.timeout = 0; @:opcoes.extendedtimeout = 0; } else { @:opcoes.timeout = 10000; @:opcoes.extendedtimeout = 10000; } var texto = mensagem.mensagem.replace("\r\n", "<br /> ").replace("\r", "<br /> ").replace("\n", "<br /> ").replace("'", ""); @:window.toastr['@tipoalerta']('@(new htmlstring(texto))', '@mensagem.titulo', opcoes); } }); </script> } }
2) partial view added main page (_message.cshtml):
@if (tempdata.containskey("alerta")) { alerta alerta = tempdata["alerta"] alerta; @mensagemalerta.mostrarmensagensalerta(alerta); }
3) master page, added div related partial view of messages:
<div id="div-mensagens"> @html.partial("_mensagem") </div>
4) inside controller, have method adds messages tempdata:
public mensagemalerta adicionarmensagemalerta(string mensagem, tipoalerta tipoalerta = tipoalerta.info) { string titulo = string.empty; switch (tipoalerta) { case tipoalerta.error: titulo = "erro"; break; case tipoalerta.info: case tipoalerta.success: titulo = "informação"; break; case tipoalerta.warning: titulo = "alerta"; break; } alerta alerta = tempdata["alerta"] alerta; alerta = alerta ?? new alerta(); var mensagemalerta = alerta.adicionarmensagemalerta(titulo, mensagem, tipoalerta); tempdata["alerta"] = alerta; return mensagemalerta; }
message viewing happens correctly when request post , main page (_layout.cshtml) loaded again. problem when calls made via ajax , partial view loaded or no item returned. in these conditions, if partial view "_message.cshtml" not reloaded, messages not displayed.
another problem standard error handling uses function display messages. way, when error occurs ajax call, message displayed when user reloads entire page.
a palliative solution using is, @ end of every ajax call, make new call server forcing reloading of message div, follows:
$(document).ajaxcomplete(function(event, xhr, options) { if(options.url.tostring().split('/')[2] != 'mensagens') exibirmensagens(); }); function exibirmensagens() { $("#div-mensagens").load('@url.action("mensagens")'); } public actionresult mensagens() { return partialview("~/views/shared/_mensagem.cshtml"); }
i'm not satisfied solution given, i'm forcing new call server unnecessarily.
i share problem find best way handle situation.
Comments
Post a Comment