I have a basic model which I'm trying to do some Remote validation using the remote attribute.
[Remote("IsCodeValid", "Utilities", ErrorMessage = "Code already in use")]
[Required]
public string Code { get; set; }
If I do a normal Html.Editorfor(m => m.Code) then everything works fine and I am able to successfully use the Remote Validation. So the attribute and the controller method in a normal use case is working perfectly.
The issue
I'm trying to use an EditorTemplate in which I'm using a 3rd party library to re-create the Input control. In the EditorTemplate I need to either access the Remote validation attributes such as all the "data-val" attributes and then add them on or if there is some method available that I can get a list of them and then just add them on in one go.
I have managed to get access to the Html.ViewData within the editor template.
Can someone point me in the right direction? Has someone done something like this before?
Here is the flow of my Editor template so it gives you an idea of what I'm trying to achieve.
// Start of editor template
@model String
@{
var htmlAttributes = new Dictionary<string, object>();
// Observe how I can manually add attributes to the dictionary
htmlAttributes.Add("data-val", "true");
// Able to add Css class from ViewModel
if (Html.ViewData.ContainsKey("class"))
{
var cssClass = Html.ViewData.SingleOrDefault(kvp => kvp.Key.ToLower() == "class");
htmlAttributes.Add(cssClass.Key, cssClass.Value);
}
}
@(
// At this point is where I need to pass the htmlAttributes with the [Remote] validation values so that it can be added to the input control
Html.Custom3rdPartyLibrary()
.TextEditorFor(x => Model)
.HtmlAttributes(htmlAttributes)
.Render()
)