0

我有一个真正简单的 ASP.NET MVC4 应用程序,它使用 JQuery Mobile 并显示用户列表及其信息。模型外的 3 个字段是电话号码。这些字段可以包含一个空值,所以我想链接电话号码,<a href="tel:"如果它存在,如果不存在,什么都没有。我想出了这个:

<div data-role="fieldcontain">
    <label for="textinput1"><strong>Office Phone:</strong></label>
    if(!@String.IsNullOrEmpty(user.OfficePhone)){
        <a href="tel:@user.OfficePhone"><input name="" id="textinput1" value="@user.OfficePhone" type="text" readonly="true"/></a>
    } else {
        <input name="" id="textinput" value="@user.OfficePhone" type="text" readonly="true"/>
    }
</div>

但是会认为有一种更清洁,更好的方法来做到这一点。还有其他选择吗,或者我是否坚持<input>在两种情况下都写出两次标签?

4

1 回答 1

0

这是未经测试的代码。希望它能传达这个想法:

@{string pref="tel:"}
@if(String.IsNullOrEmpty(user.OfficePhone))
{pref="";
}


<div data-role="fieldcontain">
    <label for="textinput1"><strong>Office Phone:</strong></label>
        <a href="@(pref):@(user.OfficePhone)"><input name="" id="textinput1" value="@(pref)@(user.OfficePhone)" type="text" readonly="true"/></a>
</div>
于 2013-02-04T20:45:49.050 回答