2016-12-14 46 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。據我所知,很大程度上取決於你使用哪一個。 –

相關問題