【问题标题】:All namespaces ignored in mvc razor view while debugging调试时在 mvc razor 视图中忽略所有命名空间
【发布时间】:2019-09-14 10:02:08
【问题描述】:

更新:经过多次尝试,发现连String都不能识别,只能识别字符串。

如果我从 MVC 模板创建新项目,同时在 立即窗口 中进行调试 我能做到:

var a = new List<int>{1,2,3};
a.First()

但是在我现有的项目中,如果我尝试这样做:

var a= new List{1,2,3};

它给了我

error CS0246: The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?)

另一方面,我可以做到这一点。

var a = new System.Collections.Generic.List<int>{1,2,3};

但这次

a.First() 给我

error CS1061: 'List<int>' does not contain a definition for 'First' and no accessible extension

我也有顶级剃须刀视图页面

@using System;
@using System.Linq;
@using System.Collections.Generic

事实上,过去一切都习惯于在没有对网络配置进行任何更改的情况下工作。

我尝试了几次尝试,例如thisthis one

我的现有项目可能有什么问题,我既不能使用也不能列出 Linq 表达式?

我的真实情况是,我想在调试时在剃刀视图中使用 linq lambda 表达式。


谜团解开,在我看来我有 Kendo ui mvc 流利的表达方式:

@(Html.Kendo().Window()
.Modal(true)
.Width(350)
.Height(280)
.Actions(a => a.Custom("ActivateRole"))
.Animation(true)
.Content(FormContent().ToHtmlString())
.Name("RoleSelector")
.Events(eve => eve.Activate("centerKendoWindow"))
.Title("Role selection")
)

form表单内容,我有

   @helper FormContent()
   {
   }

因此,我“失去上下文”然后忽略所有命名空间。

【问题讨论】:

  • 您在运行时遇到这些错误?还是在编译时?
  • 在断点暂停应用程序时调试运行时
  • 您使用的是哪个版本的 MVC?这在 MVC 5 中运行良好。
  • 感谢您的检查,MVC 5.2.3.0 也是如此。我注意到它曾经可以工作,但后来在相同的库中可以工作。

标签: c# asp.net-mvc debugging razor


【解决方案1】:

好吧,我刚刚在我的剃刀视图中尝试了以下一段代码,它运行良好

@using System;
@using System.Linq;
@using System.Collections.Generic;

@{ 

    var a = new List<int> { 1, 2, 3 };
    a.First();

}

看看上面的代码对你有没有帮助。其次尝试在控制器端使用 LINQ 函数,以确保您的库工作正常。

最后,如果您想找到替代方案,我建议您创建一个新的 c# 类。创建一个新文件夹,例如helper 并添加一个 c# 类,使该类静态并编写您想要的任何函数,然后在您的视图中引用它。这是一段可以帮助您的代码。

public static class CommonMethods
{

    public static int AgendaFileCount(int AgendaID)
    {
        // Some function using linq you want to do
        ApplicationDbContext db = new ApplicationDbContext();
        return db.MeetingAgendaDocument.Where(c => c.MeetingAgendaID == AgendaID).Count();
    }
}

然后在 .cshtml 视图文件中只引用此类命名空间

@using AssestsManagementWebApp.Helpers

然后在视图中的任何位置使用它,如下所示

@{
  int totalFiles = CommonMethods.AgendaFileCount(item.ID);
 }

【讨论】:

    猜你喜欢
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 2020-02-18
    • 2018-12-19
    • 1970-01-01
    • 2015-06-19
    相关资源
    最近更新 更多