0

I have an input text field, and I want to be able to restore the initial value after the user edits the text. So, I would say to add a data-{something} attribute, for instance data-init, that will hold the initial value, so it can be restored later.

Result should look like:

<input type="text" val="Some value..." data-init="Some value..." />

Now, I know I can achieve this by using:

@Html.TextBoxFor(m => m.InputText , new { data_init = Model.InputText })

But it's awful, and I have a lot of input fields with this behavior.

Also, I can't create a custom HtmlHelper because I have many input types with this behavior, it will be quite messy if I go this way...

So, what I think should be the practical solution is to use Data Annotations.

public class ExampleVM
{
    [HoldInitialValue]
    public string InputText { get; set; }
}

The [HoldInitialValue] attribute will be responsible to add the data-init="{value}" to the input tag. The {value} will be taken from the property's value, with also an option to override it.

So, how do I implement this behavior?

Thanks for the helpers.

O. Shai
  • 703
  • 7
  • 22
  • By writing a custom HtmlHelper. Do you know how to? – Ivaylo Stoev Oct 30 '17 at 15:06
  • @IvayloStoev - Yes I know, I rejected the HtmlHelper solution because I have many input types with this behavior, it will be quite messy if I go this way... – O. Shai Oct 30 '17 at 15:44
  • 1
    If you want to use data annotations like this someone has to evaluate them. This is usually a helper. I don't see any other meaningful extension point. Another option (not really the MVC way) is to do it from the client side and add the initial values when the html elements are rendered for the first time by using some javascript code and based for example on some common css class. – Ivaylo Stoev Oct 30 '17 at 15:52

2 Answers2

1

Even if you were to create a custom attribute, which would need to implement MetadataAware in order to add the value to the AdditionalValues property of ModelMetadata, you still then need to create your own extension methods to read that value and add it to the htmlAttributes.

For an example of how to implement it, refer CustomAttribute reflects html attribute MVC5.

However, that is unnecessary since HTML inputs already store the initial value. For a <input> (other than a checkbox) or <textarea> its defaultValue. For a <input type="checkbox" /> its defaultChecked and for <select> its defaultSelected.

So in the case of your @Html.TextBoxFor(m => m.InputText), your could use for example

using javascript

var element = document.getElementById ("InputText");
var initialValue = elem.defaultValue;
// reset to initial value
element.value = initialValue;

or using jQuery

var element = $('#InputText');
var initialValue = element.prop('defaultValue');
// reset to initial value
element.val(initialValue);
0

try this one , using htmlAttributes as IDictionary

@Html.TextBoxFor(m => m.InputText , new Dictionary<string,object>()
               {
                   { "data-init", Model.InputText}
               })
Z.R.T.
  • 1,543
  • 12
  • 15