在剃刀(至少在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>
我想到的[明确表达(HTTP:// haacked .com/archive/2011/01/06/razor-syntax-quick-reference.aspx /)可以工作:'0 @(Model.Work.Phone)' – rene
@rene请问在浏览器中显示的是什么?谢谢。 – jason
我没有盒子可以尝试它,但根据我记得他们没有出现。 – rene