在 .Net Core 2.2 项目中使用 View 和 ViewData 时出错 --

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

一个简短的问题。尝试从我的控制器方法 return 查看时出现以下错误。

名称视图在当前上下文中不存在

参考这张图片了解我的项目结构 -- https://pasteboard.co/Jh1AxGy.png

我的密码是

    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.");
        }
    }

有人会认为我已经在控制器中添加了所有必要的命名空间。显然不是。我有一种预感,这主要是由于我的项目结构 [控制器和 cshtml 文件]。适当的脚手架问题?什么 应该怎么做?

建议。谢谢

** 值得一提 - 我的控制器继承自 ControllerBase 而不是 Controller。

The name View does not exist in current context

那是因为你的控制器继承自ControllerBase。你可以查看下面的源代码,ControllerBase默认不包含View()方法。Controller继承自ControllerBase 并添加视图支持:

//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);       
}

根据你的这种情况,只需将 ControllerBase 更改为 Controller:

public class ProductController : Controller { }

此外,我看到你的结构包含你使用 return View(model) 的 Razor Pages.When,它会使用模型 data.Be 渲染 razor 视图确保你有一个名为 [=20] 的 razor 视图=] 而不是名为 ProductInfo.cshtml.

的剃刀页面