ViewBag does not receive the value correctly

-1

I'm trying to pass a value from Controller to View , here's how it's in View:

<div class="alert alert-danger alert-dismissable" role="alert" id="alerta">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
       <span aria-hidden="true">&times;</span>
    </button>
       @Html.Raw(ViewBag.Mensagem)
       @ViewBag.Mensagem
       @Model.Message
</div>

And here's how it's in the controller:

if (cod != null && msgm != null)
{
    ViewData["Mensagem"] = "Código: " + cod + ". Erro: " + msgm;
    obj.model.Message = "Código: " + cod + ". Erro: " + msgm;
    ViewBag.Mensagem = "Código: " + cod + ". Erro: " + msgm;
    return View(obj.model);
}

But neither of you gets the values, it always goes blank, as if you did not get anything. I do not know why it happens, I was using ajax to show the alert, I thought it might be that, but I always keep it visible, and so does the problem.

I've tried going through ViewBag , ViewData and even the same variable, none of them.

    
asked by anonymous 20.12.2018 / 14:36

1 answer

2

In the view you are calling @ViewBag.Messagem and in Controller the value is being assigned to ViewBag.Mensagem .

Try to put a breakpoint in the view's ViewBag call to see what value is appearing there.

Sometimes you need to display the value in the view with @Html.Raw:

@Html.Raw(ViewBag.Mensagem)
    
20.12.2018 / 15:19