问题
Has something changed with the rtm bits regarding the validation of models.
I have a simple viewmodel that looks like
public class ProductViewModel
{
[Required]
[DataMember(IsRequired = true)]
public int ProductTypeId { get; set; }
public string Product { get; set; }
}
(I just added the DataMember(IsRequired = true) as the error message I get says to use it to fix the problem. However no joy)
Within my controller the model state is telling me model is valid however when I try passing the model to my api using RestSharp I get the following error.
{"Message":"An error has occurred.","ExceptionMessage":"Property 'ProductTypeId' on type 'Mine.Model.Model' is invalid. Value-typed properties marked as [Required] must also be marked with [DataMember(IsRequired=true)] to be recognized as required. Consider attributing the declaring type with [DataContract] and the property with [DataMember(IsRequired=true)].","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Web.Http.Validation.Validators.ErrorModelValidator.Validate(ModelMetadata metadata, Object container)\r\n at System.Web.Http.Validation.DefaultBodyModelValidator.ShallowValidate(ModelMetadata metadata, ValidationContext validationContext, Object container)\r\n at System.Web.Http.Validation.DefaultBodyModelValidator.ValidateNodeAndChildren(ModelMetadata metadata, ValidationContext validationContext, Object container)\r\n at System.Web.Http.Validation.DefaultBodyModelValidator.ValidateProperties(ModelMetadata metadata, ValidationContext validationContext)\r\n at System.Web.Http.Validation.DefaultBodyModelValidator.ValidateNodeAndChildren(ModelMetadata metadata, ValidationContext validationContext, Object container)\r\n at System.Web.Http.Validation.DefaultBodyModelValidator.Validate(Object model, Type type, ModelMetadataProvider metadataProvider, HttpActionContext actionContext, String keyPrefix)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.<>c_DisplayClass1.b_0(Object model)\r\n at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass36
1.<>c__DisplayClass38.<Then>b__35()\r\n at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass49.<ToAsyncVoidTask>b__48()\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func
1 func, CancellationToken cancellationToken)"}
I wasnt having this problem with the rc bits but then I have only started to use the restsharp libary with the rtm bits.
Any help would be great.
回答1:
In addition to adding [DataMember(IsRequired = true)] to the property, you'll also need to ensure that the attribute [DataContract] is applied at the class level.
回答2:
The data entry DataContract is being consumed by the UI to create the data entry form and the back-end whenever the form is posted. So, is it safe to say that [DataMember(IsRequired = true)] is needed for the back-end and [Required(ErrorMessage = @"Product type is required)] is needed for form validation so you can access the error message?
I'm not sure why we have to do both. Why can't we have a single attribute to be used by front-end and server side?
来源:https://stackoverflow.com/questions/12234582/mvc4-rtm-validation-throwing-an-error