Home > OS >  ViewBag Message not displaying in view in mvc5
ViewBag Message not displaying in view in mvc5

Time:10-22

"New Movie" is not displaying in View. Instead, it is displaying "ViewBag.Message" directly. But the data in ViewModel works fine.

NewMovie Action Method:

public ViewResult NewMovie()
{
  var movieType = _context.MovieTypes.ToList();
  var viewModel = new MovieViewModel { MovieTypes = movieType };
  ViewBag.Message = "New Movie";
  return View("MovieForm", viewModel);
}

MovieForm View:

@model VideoRentalApp.ViewModel.MovieViewModel
@{
  ViewBag.Title = "Movie Information";
  Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>ViewBag.Message</h2>

CodePudding user response:

Use <h2>@ViewBag.Message</h2>. The asterisk @ denotes a variable reference. Otherwise the razor compiler interprets the string as just that, a string.

  • Related