Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

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


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


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:


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


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

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;


Not, it's not a System namespace. It's whatever namespace is specified at the top of your Book class file.
 
Share this answer
 
v2
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900