我正在从事MVC项目,并将模型添加到“模型”文件夹中。
[ProjectName] .web.Models.ProductAndClient
该文件夹已经具有另一个名为“ UserAccount”的模型
当我去控制器时,我可以实例化并正常使用模型;我从控制器访问或查看模型都没有任何问题。
但是,当我进入视图并尝试在Razor中使用该模型时,它只会显示
[ProjectName] .web.Models.UserAccount
选项。它不会拉起ProductAndClient。我还可以访问bll中的其他类。这只是View不会看到的这一类。
我已经在此Stack Overflow solution中尝试过Web配置解决方案。没用同样,该视图已经可以看到模型文件夹,而不会看到一个文件。 我还尝试过构建,清洁和重建解决方案。我尝试关闭并重新启动Visual Studio。我尝试关闭并重新启动计算机。我尝试删除并重新创建该类。我尝试从其他角度访问该类。我三重检查了所有说“公开”的内容。他们都不起作用。
就确切的“错误”而言,当我键入
时@model [projectName].web.Models.ProductAndClient
“ ProductAndClient”部分下有一个红色波浪状。它说它在名称空间中不存在。我在该项目和其他项目的其他几个页面上都使用了这种语法,因此我必须做一些随机的事情才能使它不起作用。
namespace [projectName].web.Models
{
public class ProductAndClient
{
public ClientInv Client { get; set; } //used as a model for the UI
public List<ClientInv> Clients { get; set; } //collected info
public List<ProductCommon> Products { get; set; } //used to compare description and prices
public List<SelectListItem> ProductNames { get; set; } //used for drop down
}
}
using [projectName].web.Models;
namespace [projectName].web.Controllers
{
public class InvoiceController : Controller
{
public ActionResult Index()
{
//Variables
ProductCommon productCommon = new ProductCommon();
List<string> productNames_String = new List<string>();
ProductAndClient client = new ProductAndClient();
//Other code that does stuff goes here
client.Client = new ClientInv();
client.ProductNames = productNames;
client.Products = products;
return View(client);
}
@model [projectName].web.Models.ProductAndClient
答案 0 :(得分:0)
如果关闭/打开文件无济于事,请尝试添加用法。
@using [projectName].web.Models
@model ProductAndClient
或将模型名称空间添加到web.config
<pages>
<namespaces>
<add namespace="[projectName].web.Models" />
</namespaces>
</pages>