【问题标题】:Getting Error in using View and ViewData in .NetCore 2.2 Project --在 .Net Core 2.2 项目中使用 View 和 ViewData 时出错——
【发布时间】:2020-07-10 16:15:22
【问题描述】:

一个简单的问题。尝试从控制器方法返回视图时出现以下错误。

名称 View 在当前上下文中不存在

参考这张图片来了解我的项目结构 -- 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。

【问题讨论】:

    标签: asp.net-core view controller asp.net-core-2.2 viewdata


    【解决方案1】:

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

    那是因为你的控制器继承自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 { }
    

    此外,我看到您的结构包含 Razor 页面。当您使用 return View(model) 时,它会使用模型数据呈现剃刀视图。请确保您有一个名为 ProductInfo.cshtml 的剃刀视图,而不是名为 @987654332 的剃刀页面@。

    【讨论】:

    • 嗨@Rena - 是的,我已经这样做了并且工作正常。我的文件夹还有另一个命名问题,因此影响了我的命名空间,引起了歧义
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    相关资源
    最近更新 更多