Show/hide div class 基于控制器中的变量

Show/hide div class based on the variable in the controller

我在控制器中有一个字符串查询 Rest API 存储响应

string rmID = searchLogic.GetRoomID(rmName.Substring(0, rmName.IndexOf(' '))).Result;

该字段不在模型中 class。我需要检查 Index.cshtml 和 show/hide 中的 string rmID is Empty or Null 是否为 div class

@{
    if (String.IsNullOrEmpty(rmID))
    {
        <div class="row" style="padding-bottom:2rem">

            <div class="col-md-9" style="margin-left:auto;margin-right:auto;text-align:center">
                <b>
                    **You have selected not valid room**
                </b>
            </div>
        </div>
    }
}

但是上面那个不行

使用 ViewBagrmID 的值从控制器传递到视图。

string rmID = searchLogic.GetRoomID(rmName.Substring(0, rmName.IndexOf(' '))).Result;

ViewBag.Id = rmID;

然后在视图中,使用@ViewBag.Id接收值并进行判断。

查看

@if (!String.IsNullOrEmpty(@ViewBag.Id))
    {
        <div class="row" style="padding-bottom:2rem">

            <div class="col-md-9" style="margin-left:auto;margin-right:auto;text-align:center">
                <b>
                    **You have selected not valid room**
                </b>
            </div>
        </div>
    }