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.
That is because your controller inherits from
ControllerBase.You could check the source code below,ControllerBasedoes not containView()method by default.Controllerinherits fromControllerBaseand add view support:From your such scenario,just change
ControllerBasetoController: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 namedProductInfo.cshtmlinstead of a razor pages namedProductInfo.cshtml.