0

I am trying to set the innerHTML of a div in a full razor view with the contents of a partial razor view as such:

@{
MvcHtmlString Bobo = @Html.Action("_TaskDetail_E", "ProTask");
};
detail_popup.innerHTML = @Bobo;

I try this code in my partial view it gives me an Unterminated string constant Error:

 '<p>Task Name: @Html.DisplayFor(model => model.TaskName)</p>
 <p>Task Description: @Html.DisplayFor(model => model.TaskDesc)</p>'

when I use the following code in the partial view it displays fine:

'<p>Task Name: @Html.DisplayFor(model => model.TaskName)</p>' +
'<p>Task Description: @Html.DisplayFor(model => model.TaskDesc)</p>'

If I put all the code on one line in one single quoted string it works fine also. My real partial view is many lines long and single quoting every line and putting a '+' on the end of each is pretty ugly. Can someone tell me how this is normally done?

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

You could use a string literal, as found at this answer. For example:

@@"<p>Task Name: @Html.DisplayFor(model => model.TaskName)</p>
  <p>Task Description: @Html.DisplayFor(model => model.TaskDesc)</p>"
Community
  • 1
  • 1
James Haug
  • 1,426
  • 12
  • 27
  • I get "An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code "Additional information: Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'. The inner exception is "{"\"\"

    Task Name: @Html.DisplayFor(model => model.TaskName)

    \" is not valid at the start of a code block. Only identifiers, keywords, comments, \"(\" and \"{\" are valid.\r\n"}"
    – Robert Wells Dec 14 '16 at 23:03
  • Its razor. It'll need two @@ – James Haug Dec 15 '16 at 01:16
  • @RobertWells I've updated my answer per the previous comment. – James Haug Dec 15 '16 at 17:57
  • @"

    Task Name: Submit Building Plan

    Task Description: Submit Building Plan to Appropriate authorities for approval

    "
    – Robert Wells Dec 18 '16 at 15:53
  • Hello James. I get the follow error message when I use @@": JavaScript critical error at line 602, column 1 in http://localhost:58251/ProNavView/ProNavFinalView\n\nSCRIPT1014: Invalid character @"

    Task Name: Submit Building Plan

    Task Description: Submit Building Plan to Appropriate authorities for approval

    " Any ideas?
    – Robert Wells Dec 18 '16 at 15:58