Nora
Nora

Reputation: 35

ViewBag Message not displaying in view in mvc5

"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>

Upvotes: 0

Views: 78

Answers (1)

Oliver Weichhold
Oliver Weichhold

Reputation: 10296

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

Upvotes: 1

Related Questions