1

I am trying to write a C# class that will return various code pieces to a view in MVC4. The code I am returning has both html and jquery in it. Currently I am doing the following:

output += "<table id =\"" + grid.name + "\"></table>\n";
output += "<div id=\"" + grid.pager + "\"></div>\n";
output += "jQuery(\"" + grid.name + "\") //and so on

This is my method. Output is a string. This string contains both html and jquery. I am simply returning output:

return output;

Then, in my view I am using @Html.Raw to convert the string:

@Html.Raw(MyMethod.GetString())

The problem is that the html elements a formatted correctly but the jquery elements are written to the page as plain text. Any ideas how I can get the page to turn the string into actual syntax? Should I return my string differently?

1
  • hmm, I think the problem is that I forgot script tags. Let me try that out. Commented Jul 11, 2013 at 12:51

1 Answer 1

3

As Jquery is javascript, you should wrap this code in a <script> tag:

output += "<table id =\"" + grid.name + "\"></table>\n";
output += "<div id=\"" + grid.pager + "\"></div>\n";
output += "<script type=\"text/javascript\">jQuery(\"" + grid.name + "\");</script> //and so on
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.