Validation of dynamic fields in a MVC and field metadata - asp.net-mvc-3

My model looks like
public class Template
{
Id
Title
List<Field> Fields
}
The “Field” Entity contains information like Name, Caption, Type (TextBox/Select/Radio), Options, and validation rules (Range, Required, string length). How to set Field Properties from HTML helper??
The standard validation in MVC is based on DataAnnotations, but I wants to validate (Both client and Server Side) the form dynamically based on Field Metadata which is dynamic and configurable. How to set Field Properties from HTML helper so that I can use it in controller for validation?

Related

Using DataAnnonations constraints on a Data Transfer Object(dto) Model to update a Domain Model

i have a question.
i have a form which returns a CustomerDto model when submitted, i want to add some constraints like required, minlength etc to perform validation on the form, but the domain model class(Customer) that the customerDto model would map the form data to when saving does not have these data annotations constraints, is it possible for me to
add these constraints in the CustomerDto model only and perform server side validation with them and then map the CustomerDto model to the Customer model(domain) which do not have these constraints?
I do not want to alter the existing database structure by adding constraints to the Domain model customer class, but i still want to use CustomerDto model class to perform server side validation

Is there another approach or io.swagger annotation that hides fields like how #ApiModelProperty does to hide fields of a custom Object?

I would like to hide for example the id field but NOT by annotating the field itself using #ApiModelProperty but at the ParameterMapper object level. That is, annotating ParameterMapper, since this object is used in multiple places and I am not showing the same fields in all place of the swagger doc.

Enforcing data contracts on posts in odata

What is the best way to enforce required fields, field lengths, and other validation on put and post requests for WebAPI based OData requests?
I have had some success with [Required] attributes, but in the past we have used [DataContract] and [DataMember(IsRequired=true), on the entity classes, but that doesn't seem to cause the ModelState.IsValid to return false when a field with the [DataMember(IsRequired=true) is left off the json posted to the request.
It appears that the proper solution is to add both the [DataMember(IsRequired=true)] and the [Required] attributes to each data item you wish to enforce as required.

Custom Data Annotation Attribute using Metadata

My requirement is to create a custom data annotation attribute for my project. The requirement is to validate the min/ max length of a specific product from the database, which will be retrieved from the database using the ProductID. I have a dynamic page for each product where there are two fields called max length & min length. User inputs the values in these two fields which needs to be validated from the database. Product table contains all the products & one will be selected by passing a productId.
Please suggest some pointers to implement the above.
Thanks in advance.
This validation you can do only in the server side and not in client, so I see two options.
Remote Validation - You can use remote validation when you want to perform the validation and show the error message through ajax.
IValidatableObject - By implementing this interface in the class you can do both the validations at the same time and return all the validation error messages as a collection. By this way the validation will happen after the form is normally submitted.

MVC3, Models, Create & Edit Hidden Fields

I have a few models in my MVC3 web app that have fields that need to be set "behind the scenes" when a user creates or edits an object/entity.
I'm trying to figure out what the best practice is regarding these types of fields.
For example...
public class EntityA {
public int Id { get; set; }
public string Title { get; set; }
...
[ForeignKey("User")]
public int UpdatedBy_Id { get; set; }
public virtual User UpdatedBy { get; set; }
}
The create and edit views for this allow the user to edit the "Title" field, but the "UpdatedBy" field needs to be set by the app when the entity is inserted or updated.
Is it best to drop a hidden field on the views and set "UpdatedBy_Id" there, or use the model property "get/set" body to do so? ...or... Should this be on the HttpPost in the controller?
This is where DTOs (Data Transfer Objects) come in handy.
Your view uses a DTO as it's model. The DTO mirrors your entity object in terms of properties, but excludes properties which you don't want the user to be able to manipulate.
Then in your controller when you are ready to persist the Entity, you create a new Entity object, and take the properties from the DTO passed to the action and copy them to your Entity object. It is at this point you can set the UpdatedBy property.
To make life easier when mapping properties from the Entity to the DTO (and vice versa), you can look at AutoMapper, which will handle this automatically, if you use the same names for your properties.
If you just pass the Entity to the view, there is the potential for the user to change the values of properties that you don't want them to be able to.
I'd prefer to place fields like this outside of user control. Especially if they're integer fields a user can edit to make phony records. The choices then fall between using TempData(if session is enabled) or possibly retrieving it on the fly for the current user. If you're not worried about the user modifying them, then I'd go with a simple hidden field or placing it in the route values for the post, allowing the framework to do the work for you.
I'd say use a hidden field and set the UpdatedBy_Id. It will then be posted back with the form and it can be databound like the rest of the information.

Resources