asp.net-coreviewcontrollerasp.net-core-2.2viewdata

Getting Error in using View and ViewData in .NetCore 2.2 Project --


a quick question. I am getting following error when trying to return view from my controller method.

The name View does not exist in current context

refer this pic to understand my project structure -- https://pasteboard.co/Jh1AxGy.png

My code is

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using AutoMapper;
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Authorization;
    using CoreSpeechService.App.Utility;

    public IActionResult ProductInfo(int p_id)
    {
        if(p_id>0)
        {
            var pEntity = pService.GetProductById(p_id);
            if(pEntity!=null)
            {
                ViewData["ProductId"] = pEntity.Id;
                return View(pEntity);
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
        else
        {
            return BadRequest("Could not fetch detailes related to this product at the moment. Please try later.");
        }
    }

One would have thought I have added all necessary namespaces in controller. Apparently not. I have a hunch it is mostly due to my project structure [the controller and cshtml files]. Proper scaffolding issue? What should be done?

Suggestions. Thanks

** worth mentioning - my controller is inherited from ControllerBase and not Controller.


Solution

  • The name View does not exist in current context

    That is because your controller inherits from ControllerBase.You could check the source code below,ControllerBase does not contain View() method by default.Controller inherits from ControllerBase and add view support:

    //A base class for an MVC controller with view support.
    public abstract class Controller : ControllerBase, IActionFilter, IFilterMetadata, IAsyncActionFilter, IDisposable
    {
        protected Controller();
         
        // Summary:
        //     Creates a Microsoft.AspNetCore.Mvc.ViewResult object that renders a view to the
        //     response.
        //
        // Returns:
        //     The created Microsoft.AspNetCore.Mvc.ViewResult object for the response.
        [NonAction]
        public virtual ViewResult View();
        //
        // Summary:
        //     Creates a Microsoft.AspNetCore.Mvc.ViewResult object by specifying a viewName.
        //
        // Parameters:
        //   viewName:
        //     The name or path of the view that is rendered to the response.
        //
        // Returns:
        //     The created Microsoft.AspNetCore.Mvc.ViewResult object for the response.
        [NonAction]
        public virtual ViewResult View(string viewName);
        //
        // Summary:
        //     Creates a Microsoft.AspNetCore.Mvc.ViewResult object by specifying a viewName
        //     and the model to be rendered by the view.
        //
        // Parameters:
        //   viewName:
        //     The name or path of the view that is rendered to the response.
        //
        //   model:
        //     The model that is rendered by the view.
        //
        // Returns:
        //     The created Microsoft.AspNetCore.Mvc.ViewResult object for the response.
        [NonAction]
        public virtual ViewResult View(string viewName, object model);
        //
        // Summary:
        //     Creates a Microsoft.AspNetCore.Mvc.ViewResult object by specifying a model to
        //     be rendered by the view.
        //
        // Parameters:
        //   model:
        //     The model that is rendered by the view.
        //
        // Returns:
        //     The created Microsoft.AspNetCore.Mvc.ViewResult object for the response.
        [NonAction]
        public virtual ViewResult View(object model);       
    }
    

    From your such scenario,just change ControllerBase to Controller:

    public class ProductController : Controller { }
    

    Besides,I see your structure contains Razor Pages.When you use return View(model),it would render the razor view with the model data.Be sure you have a razor view named ProductInfo.cshtml instead of a razor pages named ProductInfo.cshtml.