使用Razor条件语句防止换行

时间:2015-06-05 19:54:21

标签: c# asp.net-mvc razor

我想根据html对象中包含的某个变量,使用razor语法显示一些ViewBag内容。当前内容在条件语句之后中断到换行符。这就是我所拥有的:

<h5>Apply salary @ViewBag.salaryStatus to associated objects?</h5>
    @if (ViewBag.salaryStatus == "Executed")
    {
        <h5>(This will overwrite old values of the selected objects).</h5>
    }

实际结果是:

Apply salary reviewed to associated objects?
(This will overwrite old values of the selected objects).

预期结果是:

Apply salary reviewed to associated objects? (This will overwrite old values of the selected objects).

2 个答案:

答案 0 :(得分:4)

您正在添加多个H5元素。这些是块HTML元素,默认情况下将放在新行上。你应该只创建一个H5元素:

<h5>Apply salary @ViewBag.salaryStatus to associated objects?
@if (ViewBag.salaryStatus == "Executed")
{
    <text>(This will overwrite old values of the selected objects).</text>
}
</h5>

答案 1 :(得分:0)

你也可以使用以下逻辑;

@{
   var message = "";
   if () {
      message = "your message";
   }
}
<h5>Apply salary @ViewBag.salaryStatus to associated objects? @message</h5>