So in my mvc project's Project.Repository I have
[MetadataType(typeof(FalalaMetadata))]
public partial class Falala
{
public string Name { get; set; }
public string Age { get; set; }
internal sealed class FalalaMetadata
{
[Required(ErrorMessage="Falala requires name.")]
public string Name { get; set; }
[Required(ErrorMessage = "Falala requires age.")]
public string Age { get; set; }
}
}
I use Falala as a model in my Project.Web.AccountControllers, and use a method to get violations.
Validating worked when I had
public class Falala
{
[Required]
public string Name { get; set; }
[Required(ErrorMessage="error")]
public string Age { get; set; }
}
but not after using the partial class from above.
I really need to use a partial class. What am I doing wrong here?
Thanks!
I tend to use Metadata classes as followed.
[MetadataType(typeof(FalalaMetadata))]
public partial class Falala
{
public string Name { get; set; }
public string Age { get; set; }
}
public class FalalaMetadata
{
[Required(ErrorMessage="Falala requires name.")]
public string Name { get; set; }
[Required(ErrorMessage = "Falala requires age.")]
public string Age { get; set; }
}
Which works fine for me.
The following should also work (and is a better way to implement metadata classes):
[MetadataTypeAttribute(typeof(Falala.FalalaMetaData))]
public partial class Falala
{
internal sealed class FalalaMetadata
{
[Required(ErrorMessage="Falala requires name.")]
public string Name { get; set; }
[Required(ErrorMessage = "Falala requires age.")]
public string Age { get; set; }
}
}
I ran into a similar problem and finally got it working by putting both the Model class and the Metadata "buddy" class in the same namespace, even though my references seemed ok. I'm kind of a .net noob though so I'm not exactly comfortable with namespaces, could be something else.
Could Internal on the nested class be the reason...?
I had a similiar problem and it seemed to all boiled down to not making the individual fields in the nested metadata class public - wonder if making the whole class internal causes the same problem?
Not sure if this help, but I had a similar problem and spend days on it. At the end it was just a minor change which did the trick for me.
I changed UnobtrusiveJavaScriptEnabled to false in the config file
Good luck
Related
I am using Visual Studio Express 2013 for Web (specifically version 12.0.21005.1 REL). This is my first project using VS2013, I've been using VS2012 up until this point.
I am attempting to create a new controller in my asp.net MVC application. I am using Entity Framework 5 with code first (.NET 4.5). I want Visual Studio to create the template for me (you know, a controller with views to read/write/delete etc, rather than write the code myself from scratch).
However, every time I try to create the controller I get the following error message:
Is there some sort of bug in VS 2013? I can't figure out what this means, and restarting VS2013 does not help.
Here are the gory details.... actually it is all very simple since this is a new project with very little code written so far.
My model:
namespace ProfessionalSite.Models
{
public class EntityModels
{
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
public class Enrollment
{
public int ID { get; set; }
public string EnrollmentName { get; set; }
public string Credits { get; set; }
}
// Create the class that inherits from DbContext
// The name of this class is also used as the connection string in web.config
public class EFDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
}
}
}
And in my web.config file I have the following
<add name="EFDbContext"
connectionString="Data Source=JONSNA\SQLEXP2012WT;Initial Catalog=ProfessionalSiteDb; Integrated Security=True; MultipleActiveResultSets=True"
providerName="System.Data.SqlClient"/>
within the tags.
Now time to create a controller. I right click on Controllers in the Solution Explorer, and choose to Add a new Controller.
And then
And when I click Add I get
I cant figure out how to get rid of this error. I guess as a workaround I can just type the code myself, but I'd like to know if this is a bug or something I have done wrong. In VS2012 this just worked...
I'd appreciate any help or pointers. Thanks.
You don't need the EntityModels class, See below:
namespace ProfessionalSite.Models
{
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
public class Enrollment
{
public int ID { get; set; }
public string EnrollmentName { get; set; }
public string Credits { get; set; }
}
// Create the class that inherits from DbContext
// The name of this class is also used as the connection string in web.config
public class EFDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
}
}
Then when you create a controller, just select the Student or Enrollment for the Model class.
I have an abstract class and other classes that inherit from it.
Those classes are below:
[Table("Contents", Schema="Admon")]
public abstract class Content
{
public Content()
{
this.EntryDate = DateTime.Now;
}
[Key]
public int ID { get; set; }
public string Title { get; set; }
public int? ParentID { get; set; }
[StringLength(15)]
public string InfoType { get; set; }
public DateTime EntryDate { get; set; }
public string Preview { get; set; }
public string Description { get; set; }
public string Link { get; set; }
public string Text { get; set; }
public string CategoryID { get; set; }
public int? DocID { get; set; }
public virtual Content Parent { get; set; }
public virtual ICollection<Content> Subs { get; set; }
}
public class Photo : Content { }
public class Notice : Content { }
public class Article : Content { }
public class Calendar : Content { }
My problem is that anytime i run my app it throws an exception that reads
System.MissingMethodException: Cannot create an abstract class
What can I do to rectify this error.
Thanks in advance
To use inheritance in Entity Framework, you have to implement TPH (Table per Hierarchy) or TPT (Table per Type) database structure.
With this strategy, you will be able to implement your expected behavior.
You can follow this article to implement TPH or TPT, and learn about this technology.
Hope it helps !
You cannot create an instance of an abstract class. An abstract class contains abstract members which define what a subclass should contain.
If this class must remain abstract you need to create a second class that inherits from it and implements it's members and use that class to do your processing with.
If the class doesn't have to remain abstract (I can't see why it should be but without seeing the rest of your code I can't be 100% sure) then just remove the abstract keyword.
The class Content would not compile because the your class is abstract. The MVC engine
cannot create an instance of Content directly.Unless you give it some way to know which type of Problem to instantiate, there's nothing it can do.
It is possible to create your own ModelBinder implementation, and tell MVC to use it. Your implementation could be tied to a Dependency Injection framework, for example, so that it knows to create a Content1 whenever a Content class is requested.
I'm searching for a good way for managing my view models and especially the classes used in these view models. I explain below with an example:
Let's say I would like to display a view which contains a project (title, content, category, ...) and below it a list of some related projects (of the same category). I created a view model especially for this view. Here it is:
public class ProjectDetailsViewModel
{
public ProjectFullViewModel OneProject { get; set; }
public IEnumerable<ProjectLightViewModel> RelatedProjects { get; set; }
// Below are the classes used in this view model
public class ProjectFullViewModel
{
public int ProjectID { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public string Category { get; set; }
public string Client { get; set; }
public int Year { get; set; }
public IEnumerable<Technology> Technologies { get; set; }
public byte[] ScreenshotData { get; set; }
public string ScreenshotName { get; set; }
public int ScreenshotLength { get; set; }
public string ScreenshotType { get; set; }
public byte[] BackgroundData { get; set; }
public string BackgroundName { get; set; }
public int BackgroundLength { get; set; }
public string BackgroundType { get; set; }
}
public class ProjectLightViewModel
{
public int ProjectID { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public string Category { get; set; }
public string Client { get; set; }
public string Year { get; set; }
}
}
As you can see, all classes used in this view model are contained in it. I think this is easier to keep en eye on what is used. What do you think? Is it a good/bad practice? Some suggestions? I noticed that when we have a lot of view models and classes used we can be a little confused. Don't blame me, I'm still learning ASP.NET MVC and I would like to make good choices...
Thanks.
I wouldn't use nested classes for view models. In order to reference them you will have to always specify the base class. That's could be particularly annoying when writing the mappings between your domain models and view models. If you are afraid of having many files containing your view models you could still place all the dependent view models inside the same .cs file as the parent model.
That is good practice. It is the use of View Models that allow you to pass or retrieve the necessary data to or from a View. That's what precisely makes them view models. Classes designed specifically for a View.
As for the concern of "too many" or confusion, that just comes down to your project/solution organization. Have enough logical separation so that it is evident where all your classes live.
I have a State model class:
public class State
{
public int Id { get; set; }
public string Name { get; set; }
public int CountryId { get; set; }
public virtual Country Country { get; set; }
}
And I am trying to create a Repository:
Scaffold Repository State
I've got in generated file:
public IQueryable<State> All
{
get { return context.State; }
}
instead of context.StateS.
Property
public DbSet<State> States { get; set; }
successfully has been added to the DbContext class.
I have no overrided OnModelCreating method.
Sometimes I mention such problem in different projects but can not find a reason.
I know I've had problems with namespace confusion when using the word "State" for my database tables and POCOs. So to make things easier, I rename those to something else, like USState or StateCode. That could be what's going on here for you and the scaffolding.
I know this works for single properties as per Scott Guthrie's blog to auto-magically use a partial view to render a partial model passed to it (UI Helper like in dynamic data):
[UIHint("StateDropDown")]
public string State { get; set;}
But how do you annotate an entire class to use an UI helper like this:
[UIHint("Address")]
public class Address {
public string addr1 { get; set; }
public string addr2 { get; set; }
public string city { get; set; }
[UIHint("StateDropDown")]
public string state { get; set; }
public string zip { get; set; }
}
(Except [UIHint("Address")] doesn't seem to work on classes. I see in his examples, he has "Customer.aspx" in the Shared->EditorTemplates folder, so I assume this is possible.
Make a template with the name of the class, and it just works.