Reputation: 16469
I have a partial view that displays different buttons under certain circumstances. However, when I change this if statement I get different results even though I believe these if statements technically do the same thing. How could I fix this so the if statement is fully functional? If needed, I'll post up the entire view as well. Just please request it in comments!
Basically, when I toggle my database values to toggle "ButtonStatus" between 0 and 1, I want to switch what I want to do in the if statement.
My partial view:
@if(ViewBag.ButtonStatus != 0)
{
....
//display 1 or 2 buttons
....
....
}
else {
....
// do not display buttons
....
}
Currently with this if statement, display 1 or 2 buttons works fine (ButtonStatus = 1). However, when ButtonStatus = 0, 2 buttons are still being displayed. Meaning, the logic never goes into the else part of the if statement. I even put an alert inside the GrabGhCsStatus function and the alert goes off =/
When I change the partial view to
My changed partial view:
@if(ViewBag.ButtonStatus == 1)
{
....
//display 1 or 2 buttons
....
....
}
else {
....
// do not display buttons
....
}
None of the buttons show even if ButtonStatus = 1. I put an alert in the else part of the if statement and the alert pops up. But when ButtonStatus = 1, the else portion should not be called
Controller:
public class GhCsStatusController : Controller
{
public ActionResult Index()
{
ViewBag.CheckIfCsIsRunning = GhCsStatusProvider.GetGhCsStatus()["CheckIfCsIsRunning"];
ViewBag.CheckIfGhIsRunning = GhCsStatusProvider.GetGhCsStatus()["CheckIfGhIsRunning"];
ViewBag.CsStatus = GhCsStatusProvider.GetGhCsStatus()["CsStatus"];
ViewBag.GhStatus = GhCsStatusProvider.GetGhCsStatus()["GhStatus"];
ViewBag.ButtonStatus = GhCsStatusProvider.GetGhCsStatus()["ButtonStatus"];
return View();
}
}
Provider:
public static class GhCsStatusProvider
{
public static Dictionary<string,int> GetGhCsStatus()
{
using (Entities db = new Entities())
{
int ButtonStatus;
System.Data.Objects.ObjectParameter CsOut = new System.Data.Objects.ObjectParameter("CsStatus", typeof(int));
System.Data.Objects.ObjectParameter GhOut = new System.Data.Objects.ObjectParameter("GhStatus", typeof(int));
System.Data.Objects.ObjectParameter CsRunValue = new System.Data.Objects.ObjectParameter("CSRunningValue", typeof(int));
System.Data.Objects.ObjectParameter GhRunValue = new System.Data.Objects.ObjectParameter("GHRunningValue", typeof(int));
int r = db.proc_GhCsStatus(CsOut, GhOut, CsRunValue, GhRunValue);
Dictionary<string, int> Status = new Dictionary<string, int>();
Status.Add("CsStatus", (int)CsOut.Value) ;
Status.Add("GhStatus", (int)GhOut.Value);
Status.Add("CheckIfCsIsRunning", (int)CsRunValue.Value);
Status.Add("CheckIfGhIsRunning", (int)GhRunValue.Value);
if (Status["CheckIfCsIsRunning"] == 0 && Status["CheckIfGhIsRunning"] == 0)
{
ButtonStatus = 0;
Status.Add("ButtonStatus", ButtonStatus); // 0 --> do not display
}
else
{
if (Status["CheckIfCsIsRunning"] == 1 || Status["CheckIfGhIsRunning"] == 1)
{
ButtonStatus = 1;
Status.Add("ButtonStatus", ButtonStatus); // 1 --> display
}
}
return Status;
}
}
}
Upvotes: 1
Views: 173
Reputation: 1049
try to convert the ViewBag.ButtonStatus to string in your View to see if it catches it. I'm not 100% sure but give it a shot. Something like this... @if(ViewBag.ButtonStatus.ToString() == "1")
Upvotes: 1