0

I've inherited some code... and am trying to convert it to use MVC 3, with Razor, the VBHTML is as follows:

    For Each Message As MessageDetailsModel In Model.Messages
        @<div id='@Message.HeaderId' class='@Message.HeaderCss' onclick=@(String.Format("shMsgTree('{0}','{1}',{2},'{3}');", Message.HeaderCss, Message.HeaderId, Message.MessageId, Message.UserId))>
            ... more stuff...
        </div>
    Next

Stepping through the code, the String.format resolves to this:

shMsgTree('sh_msg_GridItem sh_msg_MessageRead ','divHeader0',40,'{85A433F0-4054-42E7-B778-3EF005E411D3}');

which is what I want on the page, but for some reason, it gets output on the page as this:

shMsgTree('sh_msg_GridAltItem" sh_msg_MessageRead="

The properties on the model are all strings.

Am at a bit of a loss as to how to get it to render. Originally the entire onclick javascript was being returned in the Model, but that didn't render any better either.

Any suggestions would really be welcome. Thanks!

3
  • Just for sanity checking, can you try removing that string.Format out into a normal text block on the page - e.g. what does it spit out inside an HTML <p>? Maybe also spit out the Message.xxx components too. (One thing to watch out for here is that unless you are using Raw output then @ will HTML format your string - but that's not necessarily happening here) Commented Nov 25, 2011 at 9:26
  • Splitting them, and rendering like this (even without Raw) ie. <p> @Model.HeaderCss </p> <p> @Model.HeaderId </p> <p> @Model.MessageId </p> <p> @Model.UserId </p> gives this: ` <p> sh_msg_GridAltItem sh_msg_MessageRead </p> <p> divHeader1 </p> <p> 36 </p> <p> {85A433F0-4054-42E7-B778-3EF005E411D3} </p>` Which is right Commented Nov 25, 2011 at 9:39
  • I have taken it a step further, and output the values into javascript variables, and then passed those in... and it seems to work now. I don't like the solution much though! var @(Model.HeaderId)_v1 = '@Model.HeaderCss'; etc... then to call it: onclick="javascript:shMsgToggle(@(Model.HeaderId)_v1, @(Model.HeaderId)_v2, @(Model.HeaderId)_v3, @(Model.HeaderId)_v4)"> YUK! :( Commented Nov 25, 2011 at 9:47

2 Answers 2

1

Given our conversation in the comments and the fact that the original Razor is quite hard to read, I think I'd recommend either:

Without stepping through it in code, it's too hard to read the syntax as currently written - so break it out into a separate compact, testable, readable component.

Hope that helps

Stuart

Sign up to request clarification or add additional context in comments.

Comments

0

Not sure if this will do it, but your onClickcode needs to be wrapped in quotes, before and after: onClick="@(String.Format(...))"

2 Comments

Unfortunately, not, no :( I now have it set to this: <div id='@Model.HeaderId' class='@Model.HeaderCss' onclick="@Html.Raw(String.Format("shMsgToggle('{0}','{1}',{2},'{3}');", Html.Raw("'" & Model.HeaderCss & "'"), Html.Raw(Model.HeaderId), Html.Raw(Model.MessageId), Html.Raw(Model.UserId)))"> But even that still renders as this: <div id='divHeader1' class='sh_msg_GridAltItem sh_msg_MessageRead' onclick="shMsgToggle(&#39;&#39;sh_msg_GridAltItem sh_msg_MessageRead&#39;&#39;,&#39;divHeader1&#39;,36,&#39;{85A433F0-4054-42E7-B778-3EF005E411D3}&#39;);"> ARGH! :)
Believe it or not, Razor seems to wrap the onclick events with quotes anyway (or it is for me in this instance).

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.