Reputation: 1150
I have these 2 lines in the .cshtml:
<li><a href="@Url.Action("GetAllVehicleLocations", "VehicleReporting", new { @class = "page-scroll" })">All Vehicle Locations</a></li>
<li><a href="@Url.Action("GetToBeDoneVehicles", "VehicleReporting", new { @class = "page-scroll" })">To Be Done Vehicles</a></li>
I want to hide these hyperlinks depending on a value returned from the Controller. The value is a ClientID. If ClientID = 1 then hide the links else leave them visible.
I have tried various different implementations, below being my last.
.cshtml:
if (@Html.Action("GetSelectedClientID", "VehicleReporting") != 1)
{
<li><a href="@Url.Action("GetAllVehicleLocations", "VehicleReporting", new {@class = "page-scroll"})">All Vehicle Locations</a></li>
<li><a href="@Url.Action("GetToBeDoneVehicles", "VehicleReporting", new {@class = "page-scroll"})">To Be Done Vehicles</a></li>
}
Controller:
[Authorize]
[HttpGet]
public ActionResult GetSelectedClientID()
{
selectedClientId = HelperMethods.GetClientId();
return PartialView(selectedClientId);
}
Any help is appreciated. Please note that I'm new to MVC!
Upvotes: 0
Views: 151
Reputation: 62488
Your controller should return plain text in that case or you could return json and use an ajax call for that, but the following should keep you going:
[Authorize]
[HttpGet]
public ActionResult GetSelectedClientID()
{
var selectedClientId = HelperMethods.GetClientId().ToString();
return Content(selectedClientId);
}
and now in view you can check for the returned string value:
@if(Html.Action("GetSelectedClientID", "VehicleReporting").ToString() != "1")
{
<li><a href="@Url.Action("GetAllVehicleLocations", "VehicleReporting", new {@class = "page-scroll"})">All Vehicle Locations</a></li>
<li><a href="@Url.Action("GetToBeDoneVehicles", "VehicleReporting", new {@class = "page-scroll"})">To Be Done Vehicles</a></li>
}
Upvotes: 1
Reputation: 18265
If I understand correctly you want to conditionally hide parts of your view depending on a value.
You could use ViewData
dictionary in your main action:
[Authorize]
[HttpGet]
public ActionResult MyAction()
{
ViewData["selectedClientId"] = HelperMethods.GetClientId();
return View();
}
And in your view check its value this way:
@if (ViewData["selectedClientId"] != 1)
{
<li><a href="@Url.Action("GetAllVehicleLocations", "VehicleReporting", new {@class = "page-scroll"})">All Vehicle Locations</a></li>
<li><a href="@Url.Action("GetToBeDoneVehicles", "VehicleReporting", new {@class = "page-scroll"})">To Be Done Vehicles</a></li>
}
Upvotes: 0