4

I created two application in VS2012

  1. Application#1. MVC 3, NET 4.5
  2. Application#2. MVC 4, NET 4.5

Now I open any .cshtml file and add the following code:

<div>
@if (true)
{
      @string.Format("{0}", "test")
}
</div>

It works just fine in Application#1 (mvc3) and I see "test" word displayed. But it doesn't work in Application#2 (mvc4).

Could anybody explain why it happens and what should be changed?

UPDATE: I've just found out one more very weird thing. If you replace @string.format("some text") with @String.format("some text") everything works fine (note uppercase String)

5
  • Are you sure Model is still null in your second test? Commented Oct 2, 2012 at 8:19
  • Yes, I'm sure. See the updated code (I replaced it with "true") Commented Oct 2, 2012 at 13:10
  • Please try my updated answer with <p>@string.Format("{0}", "test")</p> and it should work... ;) + 1 For sure ;) Commented Oct 2, 2012 at 13:53
  • Basically without HTML place holder where the TEST would display on broswer??? Browser understand HTML TAG which is missing here... Commented Oct 2, 2012 at 13:54
  • Jigar, thanks for reply. But I don't want <p> to be rendered. <text> is better solution Commented Oct 3, 2012 at 9:08

3 Answers 3

6

We had a similar issue when upgrading... it appears that simply writing @string.Format("{0}", "test") will no longer write directly onto the page as it did in MVC3. You instead have to do something like this:

<div>
@if (true)
{
      Html.Raw(string.Format("{0}", "test"));
}
</div>

or

<div>
@if (true)
{
      <text>@string.Format("{0}", "test"))</text> //added @ to evaluate the expression instead of treating as string literal
}
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

I've just found out one more very weird thing. If you replace @string.format("some text") with @String.format("some text") everything works fine (note uppercase String)
BTW, Html.Raw won't encode text. <text>...</text> solution is better one
I'll have to test that string casing that you mentioned. That would really be absurd if that made a difference. I showed both examples of emitting raw HTML and encoded HTML intentionally. Html.Raw may be a viable solution. For me, there are cases where I want to emit raw HTML. But obviously that depends on your application and scenario.
0

Are you using the Razor engine?

Since you are in a @if block you could write: string.Format("{0}", "test") rather than @string.Format("{0}", "test").

Note the @

1 Comment

It does not compile. It requires ; at the end. But once added, it does not render "test" string in output HTML
0

In Razor Syntax for Conditional check you can start a block with one @ sign and then put your Condition inside..

E.g

  @{var price=20;}
<html>
<body>
@if (price>30)
  {
  <p>The price is too high.</p>
  }
else
  {
  <p>The price is OK.</p>
  }
</body>
</html>

So in your case you are using two @ within one block. Check that part and it will be resolved.

So your code block should look like following.

<div>
@if (Model == null)
{
      <p>@string.Format("{0}", "test")</p>
}
</div>

On top of that if you put "@" two time like it is there in your code lines it will give you hint as image below. This is feature of Razor Syntax 4.0. Also you miss the ";" in you code line.

enter image description here

http://www.w3schools.com/aspnet/razor_cs_logic.asp

1 Comment

Thanks for reply. Your code compiles (string.Format("{0}", "test");), but it does not render "test" string. How should it be rendered now? And why did it work fine in MVC3?

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.