0

I have the following JSon entered in a text column in an SQL Server database:

{
 "tag" : {
  "string" : "<input type=\"text\" [attributes] />",
  "attributes" : {
   "name" : {
    "required" : true,
    "label" : "Field name",
    "description" : "Use only letters and minus sign",
    "expert" : false
   },
   "id" : {
    "required" : false,
    "label" : "Field identifier",
    "description" : "Use only letters and minus sign",
    "expert" : true
   },
   "class" : {
    "required" : false,
    "default" : "form-control",
    "label" : "Field styles",
    "description" : "These must exist in the stylesheet, leave blank if you don't know about them",
    "expert" : true
   },
   "required" : {
    "required" : false,
    "allowed" : [
     "required",
     ""
    ],
    "label" : "Is the field required?",
    "description" : "If the user must enter a value in this field, use the word required",
    "expert" : true
   },
   "pattern" : {
    "required" : false,
    "label" : "Input pattern that must be followed",
    "description" : "This is a regular expression that will validate the field's value",
    "expert" : true
   },
   "autocomplete" : {
    "required" : false,
    "default" : "off",
    "allowed" : [
     "on",
     "off",
     ""
    ],
    "label" : "Allow autocomplete?",
    "description" : "If you wish the browser's built in autocomplete to remember the answer, set this on",
    "expert" : true
   },
   "placeholder" : {
    "required" : false,
    "label" : "Hint for the user",
    "description" : "Write an example value that can be entered in this field",
    "expert" : false
   }
  }
 }
}

Ok, so basically this JSon contains the description of an input field and all its possible attributes. The string key inside the tag key, is the basic structure of the field. In another table, I have created a field based on that structure, and it has the following information in a JSon:

{
 "tag" : {
  "name" : "username",
  "id" : "username",
  "class" : "form-control",
  "placeholder" : "Enter your name"
 }
}

With C# I want to render the field to a form:

public string RenderField()
    {
        var data = JObject.Parse(this.Data); //Data is the field that contains the definition of the field
        var structure = JObject.Parse(this.FieldTypes.Datos); // FieldTypes defines the structure of each possible field type
        string tagHtml = structure["tag"]["string"].ToString(); // This is the basic form of a form field
        foreach(JProperty key in data["tag"])
        {
            tagHtml = tagHtml.Replace("[attributes]", key.Name + "=\"" + key.Value + "\" [attributes]");
        }
        return WebUtility.HtmlDecode(tagHtml).Replace("[attributes]", "");
    }

Basically everything is working as expected, but for some reason the field is not being displayed, but its htmlentities like this:

&lt;input type=&quot;text&quot; name=&quot;username&quot; id=&quot;username&quot; class=&quot;form-control&quot; placeholder=&quot;Enter your name&quot;  /&gt;

I don't know why it is not decoding the string. Any ideas?

Tales
  • 1,829
  • 3
  • 33
  • 50

1 Answers1

1

You are trying to display html and the problem is not in the method but in the fact that you are not printing it correctly in mvc. Do it like advised here:

@Html.Raw(RenderField())
Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78
  • You are correct, someone commented the same solution before, and I tried it in my view. It fixed the problem. – Tales Jul 30 '18 at 22:01