c# - 调试时在 mvc razor 视图中忽略所有命名空间
问题描述
更新:经过几次尝试,发现连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
事实上,过去一切都习惯于在没有对 Web 配置进行任何更改的情况下工作。
我既不能使用也不能列出 Linq 表达式的现有项目可能有什么问题?
我的真实情况是,我想在调试时在剃刀视图中使用 linq lambda 表达式。
谜团解开了,在我看来我有剑道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")
)
形式形式的内容,我有
@helper FormContent()
{
}
因此,为此,我“失去了上下文”,然后忽略了所有命名空间。
解决方案
好吧,我只是尝试在我的剃刀视图中遵循一段代码,它运行良好
@using System;
@using System.Linq;
@using System.Collections.Generic;
@{
var a = new List<int> { 1, 2, 3 };
a.First();
}
看看上面的代码是否对你有帮助。其次尝试在控制器端使用 LINQ 函数,以确保您的库工作正常。
最后,如果您想找到替代方案,我建议您创建一个新的 c# 类。创建一个新文件夹,例如 helper 并添加 ac# 类,使该类静态并编写您想要的任何功能,然后在您的视图中引用它。这是一段可以帮助您的代码。
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);
}
推荐阅读
- python-3.x - 如何使用 curl 将文件发送到 Google Cloud Function?
- c# - bot 框架欢迎消息。如何进行 CardAction?
- mingw - 如何更改 MinGW 中的起始目录?
- c# - 在 .NET Portable Library 项目中使用 async / await
- java - 可调用语句未在 Java 中返回响应
- javascript - 根据给定的键将对象数组转换为对象
- dart - Flutter - 从验证器更新表单状态
- android - 如何在片段中使用 viewpager 和 tablayout?
- javascript - R Shiny - 使用自定义消息处理程序禁用单选按钮组中的单个单选按钮
- vue.js - 如何在一页中使用 vuetify.css 和 bootstrap?