DMur
DMur

Reputation: 659

Display a String in Asp.Net Core 3.0 MVC ViewModel

Currently learning Asp.Net Core. I was learning with Razor pages initially and had everything working fine, but ditched them in favour of familiarizing with MVC first.

Despite online courses etc. I've been struggling for 3 days to get a simple string to display using a ViewModel and now my mind is frazzled!

I want to inject a string into my cshtml page, so that the string can come from a strings library. I had all this all setup and working fine with Razor pages, but I cannot get a simple string to display using MVC unless I use ViewBag. I want to use ViewModel. What am I missing?

Any help would be much appreciated.

AboutViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyApp.Models
{
    public class AboutViewModel
    {
        public string Title { get; set; }

        public void OnGet()
        {
            Title = "Test";
        }
    }
}

AboutController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
using MyApp.Models;

namespace MyApp.Controllers
{
    public class AboutController : Controller
    {
        public IActionResult About()
        {
            return View();
        }
    }
}

About.cshtml

@model AboutViewModel
    @{
        ViewData["Title"] = _loc["About Us"];
    }

<body>
    <div>
        @Model.Title
    </div>
</body>

Error:

 NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Views_About_About.<ExecuteAsync>b__12_2() in About.cshtml, line 22


NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Views_About_About.<ExecuteAsync>b__12_2() in About.cshtml
+
        @Model.Title
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.SetOutputContentAsync()
AspNetCore.Views_About_About.ExecuteAsync() in About.cshtml
+
        ViewData["Title"] = _loc["About Us"];
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0<TFilter, TFilterAsync>(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)

Thoughts:

I'm pretty sure I'm crossing over with Razor pages here and my OnGet method is useless for MVC? I have tried putting all sorts of code into my controller and nothing seems to work.

Response to Suggestion Below:

If I change my controller to:

    public IActionResult About()
    {
        var viewModel = new AboutViewModel();

        return View(viewModel);
    }

then the error disappears but the string still doesnt display.

Upvotes: 0

Views: 1165

Answers (1)

Muhammad Sami
Muhammad Sami

Reputation: 550

 public IActionResult About()
    {
        var viewModel = new AboutViewModel();
         viewModel .Title = "Test";
        return View(viewModel);
    }

Upvotes: 2

Related Questions