Validating and editing a “Changeable”/optional type in Asp.net MVC 3?

You may try using dynamic types with a custom range validation attribute: public interface IChangeable { bool Changed { get; set; } } public class Changeable : IChangeable { public bool Changed { get; set; } public T Value { get; set; } } public class MyModel { MyRange(1, 10) public Changeable SomeInt { get; set; } } public class MyRangeAttribute : RangeAttribute { public MyRangeAttribute(double minimum, double maximum): base(minimum, maximum) { } public MyRangeAttribute(int minimum, int maximum) : base(minimum, maximum) { } public override bool IsValid(object value) { var changeable = value as IChangeable; if (changeable == null ||!changeable. Changed) { return true; } dynamic dynValue = value; return base. IsValid((object)dynValue.

Value); } } then a controller: public class HomeController : Controller { public ActionResult Index() { return View(new MyModel { SomeInt = new Changeable { Changed = true, Value = 5 } }); } HttpPost public ActionResult Index(MyModel model) { return View(model); } } then a view ( ~/Views/Home/Index. Cshtml ): model MyModel @using (Html.BeginForm()) { @Html. EditorFor(x => x.

SomeInt) OK } and the corresponding editor template (notice the name of the editor template file) ~/Views/Shared/EditorTemplates/Changeable`1. Cshtml model dynamic @Html. CheckBox("Changed", (bool)Model.

Changed) @Html. Editor("Value") @Html. ValidationMessage("").

You may try using dynamic types with a custom range validation attribute: public interface IChangeable { bool Changed { get; set; } } public class Changeable : IChangeable { public bool Changed { get; set; } public T Value { get; set; } } public class MyModel { MyRange(1, 10) public Changeable SomeInt { get; set; } } public class MyRangeAttribute : RangeAttribute { public MyRangeAttribute(double minimum, double maximum): base(minimum, maximum) { } public MyRangeAttribute(int minimum, int maximum) : base(minimum, maximum) { } public override bool IsValid(object value) { var changeable = value as IChangeable; if (changeable == null ||!changeable. Changed) { return true; } dynamic dynValue = value; return base. IsValid((object)dynValue.

Value); } } then a controller: public class HomeController : Controller { public ActionResult Index() { return View(new MyModel { SomeInt = new Changeable { Changed = true, Value = 5 } }); } HttpPost public ActionResult Index(MyModel model) { return View(model); } } then a view (~/Views/Home/Index. Cshtml): @model MyModel @using (Html.BeginForm()) { @Html. EditorFor(x => x.

SomeInt) OK } and the corresponding editor template (notice the name of the editor template file) ~/Views/Shared/EditorTemplates/Changeable`1. Cshtml @model dynamic @Html. CheckBox("Changed", (bool)Model.

Changed) @Html. Editor("Value") @Html. ValidationMessage("").

Ahh :) I didn't think of @model dynamic. However, I did try @model Changeable which requires object. Thanks!

I have a lot of different validation attributes, is it possible to make a validator for Changeable which propagates the value? – lasseespeholt Nov 6 at 13:14 @lasseespeholt, I don't understand what you mean by validator for Changeable which propagates the value. What propagation?

By the way if you want to do something other than trivial validation I would recommend you using FluentValidation. NET instead of data annotations. It's an excellent library that I use with a lot of success.

Declarative validation with data annotations simply cannot cope with complex validation scenarios. – Darin Dimitrov Nov 6 at 13:15 Rephrased: Is it possible to keep the data annotations from the library such as Range instead of MyRange. Maybe by doing some custom client-validation and model-binding for Changeable?

I guess I lose client-validation by using MyRange. – lasseespeholt Nov 6 at 13:23 @lasseespeholt, of course that you will loose client side validation. You have some custom validation rule here (apply the validation only if the value of some checkbox is checked).

You will have to write custom validation logic on the client for this as well. The client cannot know about this logic. So to answer your question, no you cannot rely on the built-in attributes, you will have to write your own custom attributes and not only that but if you want client validation you will have to do this manually as well.

– Darin Dimitrov Nov 6 at 13:25 Okay, thanks :) – lasseespeholt Nov 6 at 13:30.

I'm not 100% sure, but I think what you want is: private int _changeable; public Changeable SomeInt { get { return _changeable } set { _changeable = value; Changed = true; } }.

No, I want an Asp. Net MVC editor and binding for a Changeable property. I'm sorry if it wasn't clear.

I have tried to make it more explicit. – lasseespeholt Nov 6 at 12:52.

Else if (metadata.ModelType.Name. IndexOf("Changeable") > -1 &&!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions