Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class QuestionAnswerInputModel {
- public Guid QuestionId {
- get; set;
- }
- }
- public class RatingQuestionInputModel : QuestionAnswerInputModel{
- [Required]
- [Range(1,4)]
- public int? Rating { get; set; }
- }
- public class FreeTextQuestionInputModel: QuestionAnswerInputModel{
- [Required]
- public string FreeText { get; set; }
- }
- public class QuestionAnswerModelBinder : DefaultModelBinder {
- public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
- QuestionAnswerInputModel model;
- if ((typeof(QuestionAnswerInputModel) != bindingContext.ModelType)) {
- return null;
- }
- ModelBindingContext context = new ModelBindingContext(bindingContext);
- Type typeOfModel;
- string prefix = bindingContext.ModelName;
- if (bindingContext.ValueProvider.ContainsPrefix(prefix + "." + new FreeTextQuestionInputModel().GetPropertyName(m => m.FreeText))) {
- typeOfModel = typeof(FreeTextQuestionInputModel);
- } else if (bindingContext.ValueProvider.ContainsPrefix(prefix + "." + new RatingQuestionInputModel().GetPropertyName(m => m.Rating))) {
- typeOfModel = typeof(RatingQuestionInputModel);
- } else {
- return null;
- }
- context.ModelMetadata = new ModelMetadata(new DataAnnotationsModelMetadataProvider(), bindingContext.ModelMetadata.ContainerType, null, typeOfModel, bindingContext.ModelName);
- return base.BindModel(controllerContext, context);
- }
- }
- new DefaultModelBinder().BindModel(controllerContext, context)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement