2016-12-14 26 views
0

我有一个问题,我无法从视图索引创建我的模型Samochody。我在名为Models的文件夹中创建了这个模型,所有应用都可以工作,但看起来我并不像我想象的那么聪明:D你们能帮助我吗?下面的代码ASP.NET无法从我的视图中引用模型

Samochody.cs型号:

using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Linq; 
using System.Web; 
using static WebApplication2.Models.Samochody; 

namespace WebApplication2.Models 
{ 
    public class Samochody 
    { 
    public class Car 
    { 
     public int Id { get; set; } 
     public string Brand { get; set; } 
     public string Model { get; set; } 
     public decimal Price { get; set; } 
     public DateTime Bought { get; set; } 
     public DateTime Sold { get; set; } 
    } 

} 

public class CarDBCtxt : DbContext 
{ 
    public DbSet<Car> Cars { get; set; } 
} 

}

而且Index.cshtml

@model Samochody.Models.Car //error here, can't find Samochody 
@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Index</title> 
</head> 
<body> 

<div> 

</div> 
</body> 
</html> 

这是我第二次有这种类型的错误,它由2个不同的教程。

+1

'WebApplication2.Models.Samochody.Car'(但你为什么使用嵌套类?) –

+0

嵌套类? –

+0

'class car'在'class Samochody'里面# –

回答

1

您在index.cshtml中缺少类的命名空间。因此,它需要:

@model WebApplication2.Models.Samochody.Models.Car 

或另一种是编辑的web.config文件意见文件夹并添加命名空间有:

<system.web.webPages.razor> 
    <host ... 
    <pages ... 
    <namespaces> 
    <add ... 
    <add namespace="WebApplication2.Models" /> 

,然后你赢了” t需要在每个.cshtml文件中添加命名空间。

+0

另一个选择是将此命名空间添加到_viewStart文件,而不是web.config。据我所知,很大程度上取决于你使用哪一个。 –

相关问题