Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在cshtml视图中导入c#类_C#_Asp.net - Fatal编程技术网

在cshtml视图中导入c#类

在cshtml视图中导入c#类,c#,asp.net,C#,Asp.net,这可能吗?在遍历一个列表时,当在for-each循环中存储数据时,我不能使用它的实际类作为变量类型 @using MyFirstWebApplication.Models.Student @model IEnumerable<MyFirstWebApplication.Models.Student> @{ ViewBag.Title = "Title"; } <h2>This is a heading</h2> @foreach (Student

这可能吗?在遍历一个列表时,当在for-each循环中存储数据时,我不能使用它的实际类作为变量类型

@using MyFirstWebApplication.Models.Student
@model IEnumerable<MyFirstWebApplication.Models.Student>
@{
    ViewBag.Title = "Title";
}

<h2>This is a heading</h2>

@foreach (Student student in Model)
{
    <h2>Student Name: @student.FirstName @student.LastName</h2>
}
@使用MyFirstWebApplication.Models.Student
@模型IEnumerable
@{
ViewBag.Title=“Title”;
}
这是一个标题
@foreach(模型中的学生)
{
学生姓名:@Student.FirstName@Student.LastName
}

只有当foreach循环中的变量类型为“var”时,代码才会编译。

您的@using语句看起来不正确。假设类的全名为MyFirstWebApplication.Models.Student,则using语句应为:

@using MyFirstWebApplication.Models

您的using语句看起来有点不正确。 试试这个:

@using MyFirstWebApplication.Models
@model IEnumerable<MyFirstWebApplication.Models.Student>
@{
    ViewBag.Title = "Title";
}

<h2>This is a heading</h2>

@foreach (Student student in Model)
{
    <h2>Student Name: @student.FirstName @student.LastName</h2>
}

您可以通过配置将using语句隐式添加到每个页面,而不是将using语句添加到
.cshtml
文件的顶部。那么你就不必去修改每个Razor文件了。不要重复你自己

为此,请在
~/Views
文件夹(不是应用程序根目录中的主文件夹)中找到
web.config
文件,并在适当的位置添加以下配置:

<configuration>
  <system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
         <add namespace="MyFirstWebApplication.Models" /> <!-- This will probably be the only line you need to add -->
      </namespaces>
    </pages>
  </system.web.webPages.razor>
</configuration>


完全去掉@using语句,用var替换Student。使用
var
有什么不对?哈哈,这是我注意到并做过的一件事。var没有问题!我觉得特别奇怪的是,我不能将Student用作变量类型。我希望我能投票支持你们两个:(难道我们不允许像在Java中那样导入一个特定的类吗?我总是要导入整个文件夹吗?@LinhSaysHi你没有导入任何文件夹。你只是导入名称空间。名称空间用于将类实际上分开放置。别担心。这不会影响应用程序运行时的性能。c#using语句I它与Java的非常不同。C#using语句导入整个名称空间,如@Bilal所述,而Java的导入一个类。MSDN有更多信息。@LinhSaysHi您可以对两者进行投票,但只能接受一个。如果答案基本相同,我通常会选择第一个。但是如果一个答案比另一个好,我通常会选择一个ally选择那个。这不是问题的答案,但我很高兴你把它放在那里,因为我不知道这个。(y)@BilalFazlani它没有直接回答这个问题。但潜在的问题显然是“我如何在Razor视图中引用其他名称空间中的类型?”这是一种比在每个Razor文件中添加
语句更简洁的方法。这非常有趣!我非常欣赏这个答案,因为现在我知道还有另外一种导入方法,可能更适合某些特定的应用applications@mason仔细想想,是的,我同意。这是一种更干净的方法。
<configuration>
  <system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
         <add namespace="MyFirstWebApplication.Models" /> <!-- This will probably be the only line you need to add -->
      </namespaces>
    </pages>
  </system.web.webPages.razor>
</configuration>