I have a ViewModel which is used for a Wizard containing two separate ViewModels which are used to load partial views into the wizard. Depending on the option selected on Step1, I need to validate either Step2 or Step3. The viewmodels for Step2 and 3 have their own AbstractValidator which works great because it includes client side validation. The problem is that if I use ModelState.IsValid, one of the VMs always fails to validate because it's empty (that step was not filled out). Is there a way to skip validation for one of the viewmodels?
Say you have the following model:
public class PersonViewModel
{
[Required]
public string Email { get; set; }
[Required]
public string Password { get; set; }
[Required]
public string FullName { get; set; }
}
But you want to exclude FullName from the model validation because you are using the model also in a place where FullName is not filled in, you can do so in the following way:
ModelState.Remove("FullName");
Hope to help, my friend :))
The solution I eventually arrived at was surprisingly simple but I didn't spot it in the docs. You can call $('#somecontainer').valid() which will show validation messages for supported controls and also return true if all controls in the container are valid. It doesn't work for everything as rules like GreaterThan aren't supported client side.
Related
This question already has answers here:
What is ViewModel in MVC?
(16 answers)
Closed 5 years ago.
I have a Account class for account models.
public class Account
{
[Key]
public Int64 UID { get; set; }
[Required]
public string ID { get; set; }
[Required]
public string PassWord { get; set; }
[Required]
public string UserName { get; set; }
}
My project is not a code first project and this is a model class for database 'Account'.
But I use only two properties in login view, string ID and string PassWord.
So I can not use ModelState.Isvalid() in the login controller when I check the validation of model because I use
just two properties...
So I searched about that then, now I found about 'ViewModel' which is the model class for view.
Then I created a new class 'AccountViewModel' and then I mapped this with view instead of 'Account' model.
Did my way was right?
I understood the ViewModel is a model class just for View.
And The model class is for all. (like a global meaning...? for DB,view and so on)
What is different between Model and ViewModel class?
May I get some a nice way to solve this?
As the name says, view model is very specific to the view.It will be a simple POCO with only those properties needed for the view.
Your other model class is your entity models. So if you are using EF code first approach, you need entity class definitions from which EF will generate the database tables. So basically these entity classes look very similar to your db schema structure.
By creating a view model, you are removing the strong coupling of your entity classes to the UI layer. Now your UI layer is independent of your entity classes and if you ever decide to change the data access code from EF to something else, you do not need to touch the views at all.You simply need to update the mapping part(from the view model to the data access/service layer entities)
View models sometimes looks very similar to your entity models, especially if your entity model is a simple table/class.
In your case, since your view is passing a userid and password, you need a simple view model which has only those 2 properties. When user submits the form,you can read the values and use it to build an domain entity class object as needed.
public class LoginViewModel
{
public string UserId { set;get;}
public string Password { set;get; }
}
You can use data annotations with the view models. The MVC model validation framework these data annotations to do the validations. For example, since user should enter a UserId and Password, you may decorate them with appropriate annotations.
public class LoginViewModel
{
[Required]
public string UserId { set;get;}
[Required]
public string Password { set;get; }
}
The [Key] attribute is more useful when you define an entity class. So i would not think it is needed for a view model. Remember view model is more like a UI concern. It has no idea about your underlying data storage mechanism at all.
Some of the most used attributes with view model properties are
Required
MinLength
Range
Url
Phone
StringLength
DataType
I'm learning asp.net mvc and wonder when we need to use BindAttribute.
The first case (using Bind):
Model:
public class Book
{
public string Id { get; set; }
public string Name { get; set; }
public string Author { get; set; }
}
Controller:
public IActionResult Create([Bind(nameof(Book.Name), nameof(Book.Author))] Book model)
{
return Ok();
}
The book Id would be generated on server side. So, client side has nothing to do with it, every actions try to change/make the id is prevented.
The second case (not using Bind):
Model:
public class BookViewModel
{
public string Name { get; set; }
public string Author { get; set; }
}
Controller:
public IActionResult Create(BookViewModel model)
{
return Ok();
}
Because the second model doesn't contain Id property, we don't need to prevent from creating or changing.
I prefer the second. It's easy to manage model.
Is there a case we must use Bind attribute?
We use bind when we want that some properties of complex property are ignored when received on server. It could be for safety or other reasons.
When this action is executed the MVC model binder will use the request parameters to populate the user parameter's properties, as you may already know. However, the Bind attribute tells the model binder to only populate properties with names specified.
So in this case only the Username, FullName and Email properties will be populated. All others will be ignored.
See here for more details: http://ittecture.wordpress.com/2009/05/01/tip-of-the-day-199-asp-net-mvc-defining-model-binding-explicitly/
If you have situation when you only have to ignore one parametar from binding you could use Exclude property:
[Exclude]
public Entity Name {get; set;}
Bind is used to increase security and unauthorized data to be posted on server . In your model class , suppose you have content property also. if the content property is not needed in the future. Then it would be difficult for you to remove all the occurrences of that property. Here you can use bind property like this
[Bind(exclude="content")]
or you can bind only selected properties to be posted on server by including the properties like this
public ActionResult create([Bind(Include = "Name,Author")] Modelclass modelclass)
{
//Do something here
}
You can learn more about it here
Second approach is more suitable instead writing all the properties and bind them but there are some situations where you must bind user like you have a roles property or IsAdmin property in your model then you might not want that user somehow posts the IsAdmin or roles properties to the server . That's where you can use Bind attribute
I'm working with ASP.NET MVC 4, but I on't think that matters for the purpose of this question.
I have a relatively complex model for my edit view. Like this:
public class Recipe_model
{
public string Name { get; set; }
public List<Recipe_Ingredient_model> Ingredients { get; set; }
}
where Ingredients is
public class Recipe_Ingredient_model
{
public int RecipeID { get; set; }
public int? UnitID { get; set; }
public double? Quantity { get; set; }
public Ingredient_model Ingredient { get; set; }
}
which itself contains the Ingredient model.
When I make a form for this, the built-in Html.EditorFor() doesn't work for anything past the properties of the Recipe_model, so I'm using partial views to display the editor for each of the sub-models.
That works fine as far the interface goes, but when I submit the form to the controller and try to bind to the Recipe_model automatically using
[HttpPost]
public ActionResult Edit(Recipe_model model)
{
return View(model);
}
it fails because the ids of the input elements in the partial views do not conform to the correct pattern (I think ParentModel_Property).
Short from hard-coding the ids in the partial view or binding manually from the FormCollection in the controller, is there some way to get the correct ids generated in the partial view so that the model will bind automatically on submit?
This is common problem. Instead of simple partials, use EditorTemplates (special folder for models) and binding will work automatically.
For example look at this question: Updating multiple items within same view
in addition to the answer given by #WebDeveloper
you can also try and create a custom model binder though a little more complex but will add to the ease of posting and binding form value to the objects in long run
have a look here http://patrickdesjardins.com/blog/asp-net-mvc-model-binding
you will have to manually take all the form values and bind them to the model once and then you will be able to use the #HtmlFrom methods on the razor to do anything and you will get all the value inside the objects inside the action methods as you like.
My project is in ASP.NET MVC 3. In my domain I have a model object defined by Entity Framework. In order to validate properties I simply extend the generated EF object and add a metadata class which contains all my validation attributes.
[MetadataType(typeof(ContactInformationMetaData))]
public partial class ContactInformation
{
}
public class ContactInformationMetaData
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public string Phone { get; set; }
[EmailValidator]
public string Email { get; set; }
}
This works great most of the time but now I have a more complex scenario. I don't want the Phone and Email properties to be required but I want ONE of them to be required. In other words, I want to require that either email or phone or both be set, but not none.
How would I perform complex validation like this? If I create a custom validation attribute where would I put it and how would that work?
Here is another question exactly like that (even the Phone and Email example is the same):
Model Validation / ASP.NET MVC 3 - Conditional Required Attribute
Check out FluentValidation:
http://fluentvalidation.codeplex.com/wikipage?title=mvc
you can easily create this type of custom validation, it's a pretty cool validation framework in general
Ok, so this one has had me stumped for a few hours today. After trying to muster up a couple solutions, I can't seem to get it just right.
I have a model, which contains some primitive properties and some lists.
In the view, I need to be able to update any one of these lists (add / remove / update) without hitting the database and saving the changes. The save action will only happen when the save button is clicked on the page. There will be multiple forms on the page.
The forms are as follows....
The main form will just have name/description fields.
The other forms will contain the fields needed for the types in the lists.
Example Model
public class Model
{
public string Name { get; set; }
public string Description { get; set; }
public List<Type1> { get; set; }
public List<Type2> { get; set; }
public List<Type3> { get; set; }
}
After more failed attempts, I decided to do it this way:
I throw the Model into a Session variable. Then in the view I have two forms, one for each list. The forms submit to an action that retrieves the model from the session variable, inserts the record to the appropriate list, and returns the same view with the updated model.
Since using AJAX isn't a requirement, this works just how i need it to.