实体字符串未在 C# 中解码

我在 SQL Server 数据库的文本列中输入了以下 JSon:


{

 "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

   },


好的,所以基本上这个 JSon 包含输入字段的描述及其所有可能的属性。标签键里面的字符串键,是字段的基本结构。在另一个表中,我基于该结构创建了一个字段,它在 JSon 中有以下信息:


{

 "tag" : {

  "name" : "username",

  "id" : "username",

  "class" : "form-control",

  "placeholder" : "Enter your name"

 }

}

使用 C# 我想将字段呈现为表单:


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]", "");

    }

基本上一切都按预期工作,但由于某种原因,该字段未显示,但其 htmlentities 如下所示:


&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;

我不知道为什么它不解码字符串。有任何想法吗?


回首忆惘然
浏览 169回答 1
1回答

拉丁的传说

您正在尝试显示 html,但问题不在于方法,而在于您没有在 mvc 中正确打印它。像这里建议的那样做:@Html.Raw(RenderField())
打开App,查看更多内容
随时随地看视频慕课网APP