Viewbag does not receive the value correctly

Asked

Viewed 85 times

-1

I’m trying to pass a figure on Controller for View, here is how it is in the 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 the two receives the values, always stays blank, as if receiving nothing. I don’t know why it happens, I was using ajax to show Alert, I thought it could be this, but I always make it visible, and also the problem occurs.

I’ve tried going by ViewBag, ViewData and even for the same variable, none of them receives.

  • @Messaging is not the same as defined in your Viewbag controller.Message...

  • @Georgewurthmann had already corrected, but it still didn’t work.

  • 1

    @marianac_costa And you can ensure that the execution is falling even in if?

  • @LINQ can yes, debugging he falls into it.

1 answer

2


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

Try placing 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)
  • 1

    Your answer is right and should solve the question problem, but the second part is not very accurate. It is only necessary to use @Html.Raw when the text is an HTML that needs to be interpreted.

  • Sorry, when I posted the question, I had pasted the wrong part, even correcting, did not.

  • @marianac_costa So update the question with the right code.

  • I’ve already edited the new way.

  • I was able to solve, but using ajax, by no longer using Submit, but this way works with Submit, thanks.

Browser other questions tagged

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