在MVC错误中创建类的对象! C# [英] Creating an object of class in MVC error! c#

查看:71
本文介绍了在MVC错误中创建类的对象! C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是MVC的新手,但是我已经在Model文件夹中创建了一个类,如下所示:





Hi All,

I am new to MVC, But i have created a class that in the Model folder for data like so:


public class Book
        {
            public int ID { get; set; }
            public string BookName { get; set; }
            public string AuthorName { get; set; }
            public string ISBN { get; set; }
        }





我想创建这个图书模型的对象,但我一直收到这个错误:



无法找到类型或命名空间名称'Book'(您是否缺少使用指令或程序集



这里是我在cotroller中创建Book对象的方式:







I would like to create an object of this Book Model but I keep getting this error:

"The type or namespace name 'Book' could not be found (are you missing a using directive or an assembly"

here is how I am creating the Book object in the cotroller:


public ActionResult Test()
            {
                Book book = new Book();
            }





我想我可能会丢失使用System.something但是有人可以帮忙吗??



I think I might might be missing using System.something but can anybody help??

推荐答案

如果你直接在Models文件夹中创建了Book类,它可能会在不同的命名空间中。



你在哪里在Book类型下获得波浪形的红线只需右键单击Book Book并选择Resolve - >使用一些namespa ce



或者您可以自己在代码顶部使用使用行:

If you created the Book class directly in the Models folder, it'll probably be in a different namespace.

Where you get the squiggly red line under the Book type just right-click the word Book and pick Resolve -> using some namespace.

Or you can just the "using" line at the top of your code yourself:
using YourProject.Models;





不,它不是系统名称空间。它是在Book类文件顶部指定的任何名称空间。



Not, it's not a System namespace. It's whatever namespace is specified at the top of your Book class file.


问题与MVC或除.NET本身之外的任何其他内容无关。



要使您的类及其成员(当然包括构造函数)可访问,您应该将其设置为 internal (只能在同一个程序集中访问)或 public 。你已经做到了。



另外,你应该注意不同的命名空间。通常,类型名称是可见的,例如 MyNamespace.Book 。或者您可以使用使用 指令(不要与常规或别名使用语句混淆表格,但你应该明白这只是方便的问题:

http://msdn.microsoft.com/en-us/library/sf0df423.aspx [ ^ ]。



最后,如果你使用的是另一种类型汇编,你需要引用一个程序集:

http://msdn.microsoft.com/en-us/library/ms973231.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/s1sx4kfb%28v=vs.110%29.aspx [ ^ ]。



-SA
The problem is unrelated to MVC, or anything else except .NET itself.

To make your class and its member (it includes constructors, of course) accessible, you should make it either internal (accessible only in the same assembly) or public. You have done that.

Also, you should take care about different namespaces. Generally, the type name is visible is it is full, like MyNamespace.Book. Or you can use using directive (not to be confused with using statement, in regular or alias form, but you should understand that it is nothing but just a matter of convenience:
http://msdn.microsoft.com/en-us/library/sf0df423.aspx[^].

And finally, if you are using a type in a different assembly, you need to reference an assembly:
http://msdn.microsoft.com/en-us/library/ms973231.aspx[^],
http://msdn.microsoft.com/en-us/library/s1sx4kfb%28v=vs.110%29.aspx[^].

—SA


这篇关于在MVC错误中创建类的对象! C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆