for a project, I need a way to specify whether or not a property has changed.
So in a model I would like the following:
public class Changeable<T>
{
public bool Changed { get; set; }
public T Value { get; set; }
}
public class MyModel
{
[Range(1, 10)]
public Changeable<int> SomeInt { get; set; }
}
View:
@Html.EditorFor(model => model.SomeInt)
And then I would generate a editor with a textfield (the int) and a checkbox (is changed).
The validation attributes (Range etc.) should be invoked when the checkbox is checked but not when it is unchecked.
I have tried to make the above with an editortemplate for Changeable (and then comes the validation, model binding etc.) but I’m already lost by the editortemplate because it can’t be generic.
Is the solution I want possible, or is there another reasonalble elegant solution?
Right now I’m developing a solution with a property string[] ChangedProperties and a lot of Javascript to handle validation etc. but it’s rather ugly and far from done.
Thanks…
You may try using dynamic types with a custom range validation attribute:
then a controller:
then a view (
~/Views/Home/Index.cshtml):and the corresponding editor template (notice the name of the editor template file)