MVC abstract ViewModel, retain Validation Attributes (dynamically)

爷,独闯天下 提交于 2019-12-12 02:07:36

问题


hopefully I'm missing something obvious, but I have a bit of an issue with some code I've written, and it's not feeling like I've written my code correctly.

So, let's say I have the following Model:

public class SampleViewModel {
    [Required]
    public string Property1 { get; set; }
    [Required]
    [EmailAddress]
    public string Property2 { get; set; }
    public IList<AbstractModel> Items { get; set; }
}

And then I have this abstract view model:

public abstract AbstractModel {
    [Required(ErrorMessage = "This field is required")]
    public virtual string Value { get; set; }
}

And these concrete view models:

public ConcreteModel1 : AbstractModel { }
public ConcreteModel2 : AbstractModel { }

Within my Controller, I have the following code (this is actually being done elsewhere, but for this sample, this is fine):

var model = new SampleViewModel();
var fields = new List<AbstractModel>() {
    new ConcreteModel1() { Value = model.Property1 },
    new ConcreteModel2() { Value = model.Property2 },
};
model.Fields = fields;
return View(model);

Within the SampleViewModel partial view (as I have one for each view model type), I have the following:

@model SampleViewModel
@for(var i = 0; i < Model.Items; i++) {
    @Html.EditorFor(m => Model.Items[i])
}

Lets say that I also have a distinct partial view (with very different layout requirements) per each AbstractModel.

Example for the ConcreteModel1:

@model ConcreteModel1
@Html.TextboxFor(m => m.Value)

And for the ConcreteModel2:

@model ConcreteModel2
@Html.DisplayFor(m => m.Value)

This is all working, but as I've had to pass the ViewModel's properties (Property1) into the AbstractModel, I have lost the connection between the view and the underlying model. I have been able to bind the form fields back to the model, using a custom Model Binder, but the main thing I'm missing are the model validators which have been added to the SampleViewModel class.

Ideally I want this information to be available to the AbstractModel. Validation is happening, but I'm only getting basic validation on the client (via AbstractModel's Value required attribute), but I'd like to be able to pass along validation needs from my SampleViewModel into the AbstractModel.

Expectation

What I'd really like to happen is for the AbstractModel's Value property to somehow impersonate the property that is passed into it, so that it is just acting as a proxy to the original model, but has just reshaped the SampleViewModel (or specifically it's Property1 property).

So the important thing is, considering the following creation of my fields:

var fields = new List<AbstractModel>() {
    new ConcreteModel1() { Value = model.Property1 },
    new ConcreteModel2() { Value = model.Property2 },
};

How do the AbstractModels know that their Values are supposed to be: Required, and also Required and an EmailAddress, based on the properties that are used to create them?

Thank you for your input.

来源:https://stackoverflow.com/questions/42071282/mvc-abstract-viewmodel-retain-validation-attributes-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!