0

I have a query that returns results with jquerybuilder but the results are empty because it needs to convert "<" in html result like this "&lt;";. when i launch the from the search bar less than "something" it shows empty results

Here is the code that I want to add this change to:


// GET: Search/GetJsonForQuery
public JsonResult GetJsonForQuery(ObjectJson serializedJson) {

    if (serializedJson.MainObjects == null) {
        JsonResult result = Json(new {
            updated = DateTime.UtcNow.ToString("o"),
            errorNoItemSelected = true
        },
        JsonRequestBehavior.AllowGet);

        result.MaxJsonLength = Int32.MaxValue;
        return result;
    }

    if (!IsColumnSelected(serializedJson)) {
        JsonResult result = Json(new {
            updated = DateTime.UtcNow.ToString("o"),
            errorNoColumnSelected = true
        },
        JsonRequestBehavior.AllowGet);

        result.MaxJsonLength = Int32.MaxValue;
        return result;
    }
}
zine31
  • 3
  • 1

1 Answers1

0

Not quite sure of the exact question? There are a number of ways to escape/unescape HTML. The answer suggested below only requires System.Net

using System.Net;
void Main()
{
   string encoded = WebUtility.HtmlEncode("<p>hello</p>");
   
   //&lt;p&gt;take me to the river&lt;/p&gt;
   Console.WriteLine($"encoded: {encoded}");

   string decoded = WebUtility.HtmlDecode("&lt;p&gt;hello&lt;/p&gt;");
   //<p>hello</p>
   Console.WriteLine($"decoded: {decoded}");

}
public class FormatObject {

        public int Id { get; set; }

        public int IdObject { get; set; }

        public string Conditions { get; set; }

        public List<int> SelectedAttributesId { get; set; }

        public override string ToString() {
            string result = "[" + Id + "," + IdObject + ", \"" + Conditions + "\"";
            if (SelectedAttributesId != null)
                result += ", [" + string.Join(",", SelectedAttributesId.ToArray()) + "]";

            return result + "]";
        }

    }
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Rax
  • 665
  • 8
  • 19
  • this is not working in my case i have had this JsonResult result = Json(new { updated = DateTime.UtcNow.ToString("o"), errorNoItemSelected = true,input = WebUtility.HtmlEncode("<") }, JsonRequestBehavior.AllowGet); – zine31 May 08 '21 at 14:16
  • 1
    @zine31 Uh... well, as it is already encoded, in that case it sounds like you want `WebUtility.HtmlDecode("<")` to get it back to `"<"`. – ProgrammingLlama May 08 '21 at 14:19
  • always not returning results – zine31 May 08 '21 at 14:51
  • i don't think its the good class to use because i use a jsonResult and not a string ;some ideas? – zine31 May 08 '21 at 16:21
  • Can you please clarify what the issue is with an example in your original post? The encode/decode problem is not so clear. You are creating a new Json result so there is a fix to the problem. – Rax May 08 '21 at 18:30
  • if (serializedJson.MainObjects == null){ JsonResult result = Json(new { updated = DateTime.UtcNow.ToString("o"), errorNoItemSelected = true, }, JsonRequestBehavior.AllowGet); result.MaxJsonLength = Int32.MaxValue; result = WebUtility.HtmlDecode("<"); return result; } – zine31 May 08 '21 at 18:47
  • it says in visual studio impossible to convert implicitely the type "string" in System.Web.Mvc.JsonResult thank you if you can help me @Rax – zine31 May 08 '21 at 18:49
  • @zine well, now you're trying to assign a string to `result` which is a `JsonResult`, so it makes sense you'd get that error. – ProgrammingLlama May 09 '21 at 10:45
  • is it in this part where i escape a special characters in conditions thans to help me for adding it – zine31 May 09 '21 at 13:02