I'm new in asp.net mvc and first time create new web application and in my project solution right click and add new project to solution and add class library to my project,and name it Datalayer,so in Datalayercreate new class and name it PageGroup,in PageGroup want to add System.ComponentModel.DataAnnotations to name space but get this error:
A namespace cannot directly contain members such as fields or methods...
I add entity frame work to datalayer but so not work!,how can i solve that problem?
this is my class code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel;
namespace Datalayer
{
public class PageGroup
{
[Key]
public int GroupID { get; set; }
}
}
in [Key] get error.
I think you forgot to add assembly reference in your application.
Go to Reference->Add Reference-> Select System.ComponentModel.DataAnnotations assembly to your application.
Then use the namespace in your application
using System.ComponentModel.DataAnnotations;
Please find screenshot for reference
Add assembly system.ComponentModel.DataAnnotation in your current project.
Go to Add reference> Assemblies > Framework and choose system.ComponentModel.DataAnnotation
If this is not present in your current assembly then download it form nuget package manager.
Write click on your project.> manage nuget packages and search for System.componentmodel and select appropriate package and install.
Related
I have created a class library project in VS2010 ultimate with .net 4.0 and Entity fwk-4.1.
After adding a ref to the Entity framework dll, I am not able to use the Data Annotation attributes like 'Required' on class properties.
Could you please let me know what is the solution?
Required attribute is defined inside System.ComponentModel.DataAnnotations namespace and is available in System.ComponentModel.DataAnnotations assembly(dll)
1) Make sure to add a reference to System.ComponentModel.DataAnnotations assembly(dll)
2) Add a using statement to import the System.ComponentModel.DataAnnotations namespace in your class file.
using System.ComponentModel.DataAnnotations;
public class YourModel
{
[Required]
public string Name {set;get;}
}
I'm building an Umbraco 7.2.8 website
Under installation Models or Controllers folders didn't exist so I added them.
I added a model class to Models folder:
namespace MyNamespace.Models
{
public class QuoteRequest
{
public int People { get; set; }
public int Days { get; set; }
}
}
When I'm trying to use it in a view I get error The type or namespace name 'MyNamespace' could not be found (are you missing a using directive or an assembly reference?)
#using MyNamespace.Models
My controller doesn't see it either. Why is that? What do I miss?
I tried to compile the project but with no effect. The project is compiling but no MyNamespace.dll is placed in bin folder.
Did you crate those folders in a separate project. If it's the case got to "Add references" and select your other project.
If it's in the same project you need to use the same namespace as your project name/namespace.
But I advise you to create a new project, it's a cleaner architecture.
You could also add your code into the App_code folder. The code will be available anywhere in your project.
Hope it helps.
I'm learning asp.net mvc3 from w3schools and following that tutorial.http://w3schools.com/aspnet/mvc_models.asp In the section "ASP.NET MVC Models" I have created the model like this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace MvcDemo.Models
{
public class MovieDB
{
public int ID { get; set; }
public string Title { get; set; }
public string Director { get; set; }
public DateTime Date { get; set; }
}
public class MovieDBContext : DbContext
{
public DbSet<MovieDB> Movies { get; set; }
}
}
Then I was going to add a controller according to the instructions.
In the Solution Explorer, right-click the Controllers folder, and select Add and Controller
Set controller name to MoviesController
Select template: Controller with read/write actions and views, using Entity Framework
Select model class: MovieDB (McvDemo.Models)
Select data context class: MovieDBContext (McvDemo.Models)*
Select views Razor (CSHTML)
Click Add
But the problem I have is that the drop down list doesn't show MovieDB (McvDemo.Models) in Model Class and Data Context Class to be selected. Can anyone please help me? Thanks.
You should just be able to recompile (Shift-Ctrl-B) and then try it again - it will be there. Otherwise you can always just declare it yourself at the top of a blank view, but that will not provide the scaffolding that the generator does:
#model MvcDemo.Models.MovieDB;
I recompiled but that did not fix the issue for me and yes I am doing the same thing and ran into the same exact issue. The problem for me was caused by visual web developer not being able to connect to my Movies database. I had to change the definition of my connectionString within web.config like this:
<add name="MovieDBContext"connectionString="Data Source=c:\sites\w3schools_demo\MvcDemo2\MvcDemo2\App_Data\Movies.sdf" providerName="System.Data.SqlServerCe.4.0"/>
If you are having this issue you will need to change the "Data Source" path to point to your Movies.sdf database file.
Why is this? I have a feeling that it has something to do with my controller.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NidecMotorXref.MvcUI.BaseControllers;
using System.Web.Mvc;
using LINQtoCSV;
using System.IO;
[HttpPost]
public virtual ActionResult Matches(ImportModel model, string save, HttpPostedFileBase fileUpload)
{ ... }
These are all my references but File.Delete("myfileName") does not resolve while my higher up acted like it should. Even though my solution works with System.IO.File I was curious why it doesn't resolve my reference?
Controller defines a method called File, therefore the compiler will choose this method when resolving the symbol File.
When used within a class that does not include a File symbol then the compiler will fall back on resolving via the using statements, hence File.Delete working in other classes.
In ASP.NET MVC is there a way to enumerate the controllers through code and get their name?
example:
AccountController
HomeController
PersonController
would give me a list such as:
Account, Home, Person
Using Jon's suggestion of reflecting through the assembly, here is a snippet you may find useful:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
public class MvcHelper
{
private static List<Type> GetSubClasses<T>()
{
return Assembly.GetCallingAssembly().GetTypes().Where(
type => type.IsSubclassOf(typeof(T))).ToList();
}
public List<string> GetControllerNames()
{
List<string> controllerNames = new List<string>();
GetSubClasses<Controller>().ForEach(
type => controllerNames.Add(type.Name));
return controllerNames;
}
}
You can reflect through your assembly and find all classes which inherit from type System.Web.MVC.Controller. Here's some sample code that shows how you could do that:
http://mvcsitemap.codeplex.com/WorkItem/View.aspx?WorkItemId=1567
All who using this post better read this post before: using Assembly.GetCallingAssembly() not returns the calling assembly
The issue is that razor views are acting as independent dynamic assemblies and you don't get the desired assembly.
Yair
Create the property in every controller and then you get the name like this.