2017-02-16 47 views
1

我有这样的代码:链接不工作在剃刀

<a href="tel:[email protected]">[email protected]</a> 

当我使用手机一样[email protected],在@符号不突出。这在浏览器中看起来像这样:[email protected]。我的意思是代码显示,而不是电话号码。

当我把这样的空间:

<a href="tel:0 @Model.Work.Phone">0 @Model.Work.Phone</a> 

@符号被亮显,但我想0@是彼此相邻。我怎样才能做到这一点?谢谢。

+1

我想到的[明确表达(HTTP:// haacked .com/archive/2011/01/06/razor-syntax-quick-reference.aspx /)可以工作:'0 @(Model.Work.Phone)' – rene

+0

@rene请问在浏览器中显示的是什么?谢谢。 – jason

+1

我没有盒子可以尝试它,但根据我记得他们没有出现。 – rene

回答

1

在剃刀(至少在2.0或以上)可以使用一个explicit expression

<a href="tel:[email protected](Model.Work.Phone)">[email protected](Model.Work.Phone)</a> 

如可以使用直接的String.Format呼叫通过Gypsy Spellweaver

另一种方法是作为提供替代使用剃刀委托:

@{ 
    Func<dynamic, object> phoneformat = (item) => 
     { 
      // if we have a string 
      if (item is String && !String.IsNullOrEmpty(item)) 
      { 
       // check if the first is not a 0 
       if (item[0] != '0') 
       { 
        // add it 
        item = String.Format("0{0}", item); 
       } 
      } 
      else if(item is Int32) 
      { 
       /// ints never have leading 0, so add it 
       item = String.Format("0{0}", item); 
      } 
      return item; 
     }; 
} 

<a href="tel:[email protected](Model.Work.Phone)">[email protected](Model.Work.Phone)</a> <br/> 
<a href="tel:[email protected](Model.Work.PhoneInt)">[email protected](Model.Work.PhoneInt)</a> 

<a href="tel:@phoneformat(Model.Work.Phone)">@phoneformat(Model.Work.Phone)</a> <br/> 
<a href="tel:@phoneformat(Model.Work.PhoneInt)">@phoneformat(Model.Work.PhoneInt)</a> 

下面是我所使用的型号:

public class Work 
{ 
    public string Phone { get; set; } 
    public int PhoneInt { get; set; } 
} 

和填充控制器它:

public ActionResult Index() 
{ 
    var model = new MyModel(); 
    model.Work = new Work {Phone = "612345678", PhoneInt = 612345678}; 
    return View(model); 
} 

呈现的HTML内容是这样的:

<a href="tel:0612345678">0612345678</a> <br/> 
<a href="tel:0612345678">0612345678</a> 

<a href="tel:0612345678">0612345678</a> <br/> 
<a href="tel:0612345678">0612345678</a>