'Show/hide div class based on the variable in the controller
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 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>
}
}
But the above one doesnot work
Solution 1:[1]
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 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>
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Xinran Shen |
