Home > OS >  Show/hide div class based on the variable in the controller
Show/hide div class based on the variable in the controller

Time:05-12

I have a string in the controller which queries Rest API stores the response

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

This field is not in the Model class. I need to check if the string rmID is Empty or Null in the Index.cshtml and show/hide a div class

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

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

But the above one doesnot work

CodePudding user response:

Use ViewBag to pass the value of rmID from controller to the View.

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

ViewBag.Id = rmID;

Then in the view, Use @ViewBag.Id to receive the value and do judgement.

View

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

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