I have a UserFormModel which contains a UserModel which has a set of properties with the [Required] attribute set. I have read that MVC 3 out of the box will validate models within models by default. However when I submit an empty form in my view passing back a UserFormModel containing an empty UserModel the ModelState.IsValid is always true.
I have tried sending just the UserModel back to my controller and that validates ok. It just seem to be when I am working with complex models that it does not validate.
I have also tried it with the [Required] attribute on the User property within the UserFormModel (which I believe is not required for default behaviour to work) but still no validation takes place.
Any ideas on this one would be much appreciated.
public class UserFormModel
{
public UserModel User;
public IEnumerable<SelectListItem> Roles { get; set; }
}
public class UserModel : ModelBase
{
[Required]
public string UserName { get; set; }
public string Title { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
}
[HttpPost]
public ActionResult Create(UserFormModel userFormModel)
{
if (ModelState.IsValid)
{
// Do Something
}
}
You should use properties not fields. So instead of:
public UserModel User;
you should have:
public UserModel User { get; set; }
The reason for this is that the default model binder works only with properties.
I believe that validation only goes one model property deep in the model. For example if you have the following model
public class Product
{
public int ProductId { get; set; }
[Required]
public int ProductName { get; set; }
[Required]
public decimal Price { get; set; }
}
public class ProductViewModel
{
[Required]
public Product Product { get; set; }
}
The validation against the product object in the view model will work, the validation against the product class will not with one caveat. If the Product class is a POCO class used in the entity framework code first method, the validation will work against the database. Validation against a view model will only work one deep in my experience.
Related
so when implementing entity framework code first in mvc, do we separate the view restrictions from view model? this is because for database first the model is generated(so i see the reason to separate it to view model but how about code first?)
The next questions i would ask is it ok to separate view model to another folder? since by default asp.net is MVC there is no view model inside
Model <--- what is this model call? data model? domain model? business model?
public class Student
{
public int ID { get; set; }
[StringLength(250)]
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
}
View Model
public class Student
{
public int ID { get; set; }
[MaxLength(250)]
[Required]
public string LastName { get; set; }
[Required]
public string FirstMidName { get; set; }
[Required]
public DateTime EnrollmentDate { get; set; }
}
Your model that Used in mvc views is viewmodel.
your model that persist in database is domain model.
Your domain model may has some properties that you don't need use it in your client.
Your Service layer must return Dto (data transfer object) to your client and you can map dto to viewmodel .
First Question:
You should use partial class and metadata to seperate , just like below:
[MetadataType(typeof(StudentMD))]
public partial class Student
{
public class StudentMD
{
public int ID { get; set; }
[MaxLength(250)]
[Required]
public string LastName { get; set; }
[Required]
public string FirstMidName { get; set; }
[Required]
public DateTime EnrollmentDate { get; set; }
}
}
Second Question:
It's OK to add a folder name "View Model"
I did it in my project too!
I'm new to ASP.NET MVC using Entity Framework and I'm trying to create a simple login system. At the moment I have UserProfile model that I wish to model a login form off of.
UserProfile.cs
namespace MyProject.Areas.Admin.Models
{
public class UserProfile {
[Key]
public int UserID { get; set; }
[Required]
[Display(Name = "Username")]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
public string EmailAddress { get; set; }
public string Telephone { get; set; }
}
}
As my login form will only require a username and password, is it correct for me to create a separate model (for instance, a LoginModel with just those properties, or should I contine to use the UserProfile model?
It feels better for me to create a separate model to model the login submission, but then I run into the issues such as making them reference the same table?
Thanks
You should have only one Model (Domain model), but different ViewModel class.
The ViewModel will only have the properties (from the Model) needed for a certain View /Action.
To manage mapping between Model and ViewModel(s), you should look at Mapping solutions (like AutoMapper, ValueInjecter...)
It looks you should distinguish view model and domain model. Interestin discussion was here.
Let's say I got the following Entity Framework "Ruimte" model:
public class Ruimte
{
#region Constructor
public Ruimte()
{
Kenmerken = new List<Kenmerk>();
}
#endregion
#region Properties
[Key]
public int Id
{
get;
set;
}
[Required]
public string Naam
{
get;
set;
}
public List<Kenmerk> Kenmerken
{
get;
set;
}
#endregion
}
And the "Kenmerk" model looks the following:
public class Kenmerk
{
#region Properties
[Key]
public int Id { get; set; }
public KenmerkOptie KenmerkOptie
{
get;
set;
}
[Required]
public int KenmerkOptieId
{
get;
set;
}
[Required]
public string Waarde
{
get;
set;
}
[Required]
public int RuimteId
{
get;
set;
}
#endregion
}
And in my Ruimte/Create view there are 2 fields for adding a "Kenmerk". Now a "Kenmerk" can't go into the database without having a KenmerkOptieId or Waarde. So the view will reject the submit everytime I try to post the form because of the validation. Though I want a "Ruimte" to have or not to have a "Kenmerk".
So the solution I went for was having a "RuimteCreateViewModel" with the properties "Name" which was required and a list of the another copmlex class called "KenmerkCreateViewModel". Now in this last viewmodel the KenmerkOptieId and the Waarde are not required so I finally CAN submit the form.
Though I don't think this is the best solution of "skipping" the required field validators. So what is your "best practice" when the database validation is different from the view validation?
I think xVal - a validation framework for ASP.NET MVC, see http://blog.stevensanderson.com/2009/01/10/xval-a-validation-framework-for-aspnet-mvc/ is very useful for the entity framework model that you are trying to develop. Especially the use of enforcing server-side validation, it allows you to choose to validate simple property formatting rules during property setters. See http://blog.stevensanderson.com/2008/09/08/thoughts-on-validation-in-aspnet-mvc-applications/ for an explanation.
I have an order model (shown below)
public class Order
{
//[Key]
[ScaffoldColumn(false)]
public int OrderId { get; set; }
[DisplayName("Order Date")]
public DateTime OrderDate { get; set; }
public virtual ProductSelection ProductSelection { get; set; }
public virtual ShippingDetails ShippingDetails { get; set; }
public virtual BillingDetails BillingDetails { get; set; }
public virtual CardDetails CardDetails { get; set; }
public virtual AccountUser AccountUsers { get; set; }
}
As you can see is made up of a set of other models for example ProductSelection (shown below).
public class ProductSelection
{
public int SimulatorId { get; set; }
public string VersionNumber { get; set; }
[DisplayName("Quantity")]
public int Quantity { get; set; }
[DisplayName("Total Price")]
[ScaffoldColumn(false)]
public decimal TotalPrice { get; set; }
}
The issue I am having is when I post to the Controller which has a parameter of Order, I am unable to obtain any of the values from the sub-models (for example Order.ProductSelection.SimulatorId.)
Any ideas why this isn't working as I having to currently use FormCollection which isn't ideal and better messy.
Looking forward to replies
Steve
1) Silly question but just to make sure....Do you preserve values of your sub model on the view(In the form as hidden or any other input type,make sure name of your hidden are same as your properties name in the model) or in the query string.
Before giving you fully loaded model, model binder looks at different places to load your model like your form collection,rout data and query string
If you are not preserving them in any of these places then model binder has no way to find those values and give you loaded values on controller action.
Basics.. http://dotnetslackers.com/articles/aspnet/Understanding-ASP-NET-MVC-Model-Binding.aspx
2)Your example model seems fine but make sure all properties of your sub model have public access modifier and they must have set in their property declaration.
--->I had same issue before because I had private access modifier for set on those properties and I wasted whole day to figure that out.
3)If nothing works(hope that's not the case) then at last you can write your own model binder.
Here is the good post if you decide to head in that direction
http://buildstarted.com/2010/09/12/custom-model-binders-in-mvc-3-with-imodelbinder/
This is my first post (under my account) and it feels really good to participate..!!
You should apply ForeignAttribute on the ProductSelection property which points the primary key of the ProductSelection class:
[ForeignKey("SimulatorId")]
public virtual ProductSelection ProductSelection { get; set; }
Hope that helps.
I have a Person model and a student model. The student model has 2 FKs of PersonIDs; one for student and the other for parent.
My view looks like this:
#Html.EditorFor(m => m.student.Person.FirstName)
#Html.EditorFor(m => m.student.Person.DOB)
#Html.EditorFor(m => m.student.Father.FirstName)
The models would look like this:
public partial class Person
{
public int PersonID { get; set; }
[Required]
[PlaceHolder("First Name")]
public string FirstName { get; set; }
[PlaceHolder("Birth Date")]
public Nullable<System.DateTime> DOB { get; set; }
}
public partial class Student
{
public int Student_PersonID { get; set; }
public int Parent_PersonID { get; set; }
}
I want the DOB to be required field for the student but not for the parent. If I add [Required] attribute to the DOB element, then it requires it for both. Is there a way I can set a require a field on the view? or is there a way in the model or using validation attribute to do this?
fyi... i am using EF database first approach
thanks
I would suggest having the view model match the fields that are displayed in the view. If later a field is to be removed from the view, then it will also be removed from the domain model.
In this case, if your view is to display the following fields:
StudentFirstName
StudentDOB
ParentFirstName
ParentDOB
Then I would suggest having the following view:
public class PersonViewModel
{
public int StudentPersonID { get; set; }
[Required]
public string StudentFirstName { get; set; }
[Required]
public DateTime StudentDOB { get; set; }
public int ParentPersonID { get; set; }
[Required]
public string ParentFirstName { get; set; }
public DateTime ParentDOB { get; set; }
}
Or if instead you have 2 seperate views displaying:
StudentFirstName
StudentDOB
AND displaying:
ParentFirstName
ParentDOB
Then I would suggest having 2 seperate view models:
public class StudentViewModel
{
public int StudentPersonID { get; set; }
[Required]
public string StudentFirstName { get; set; }
[Required]
public DateTime StudentDOB { get; set; }
}
public class ParentViewModel
{
public int ParentPersonID { get; set; }
[Required]
public string ParentFirstName { get; set; }
public DateTime ParentDOB { get; set; }
}
Using the view models in this way will allow you to use the [Required] data annotations for the fields that require them rather than trying to create a workaround. Note that the view models are not to be confused with the domain models and therefore this data would then need to be mapped to the domain model.
Hope this helps.
If your application is a simple application you may not need to create a seperate business logic layer and most books only present MVC with simple models which may be fine. However, if you search around you will find other examples where developers recommend having a view model seperate from a business model such as this
I would also recommend reading Wrox Professional Enterprise .Net 2009 where chapters 7 & 8 give great examples of the business layer with discussions of the Transaction Script pattern, Active Record pattern and Domain Model pattern.
One way is to make a PersonRequired class that inherits from Person. Add a metadata class to PersonRequired so you have PersonRequiredMetaData and in that specific that the inherited DOB field is required. You would need to manually copy the values between the Person and PersonRequired classes or use AutoMapper. I hope there is a better answer than this!
Another option is to use FluentValidation that would let you do the validation separate from the model (doesn't use data annotations). I wonder if some people are using data annotations for database requirements and fluent validation for programmatic requirements.