lundi 31 août 2015

string.Format vs String.Format in Razor

I have to output an employee's name and dept in the view like Welcome 'employee' to 'dept'. I am using string.Format(). I was playing around with String.Format and string.Format() and saw the following. Can someone please explain why string.Format() doesn't work or what am i missing in the code? I was reading online and saw that string.Format() and String.Format() give the same result.

Doesn't work-

<div>
  <div> Welcome </div>
    <div>
       @if(employee != null)
       {
           @string.Format("{0} at {1}", employee.name, emp.dept)
       }
    </div>
</div>

Error- Unexpected "string" keyword after "@" character. Once inside code, you do not need to prefix constructs like "string" with "@".

Works:

<div>
  <div> Welcome </div>
   <div>
     @if(employee != null)
     {
        @(string.Format("{0} to {1}", employee.name, emp.dept))
     }
   </div>
</div>

Works

<div>
   <div> Welcome </div>
     <div>
        @if(employee != null)
        {
            @String.Format("{0} at {1}", employee.name, emp.dept)
        }
   </div>
</div>

Aucun commentaire:

Enregistrer un commentaire