我在.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>
我想隐藏这些超链接,具体取决于Controller返回的值。该值是ClientID。如果ClientID = 1,则隐藏链接,否则将其保持可见。
我尝试了各种不同的实现,下面是我的最后一次。
.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>
}
控制器:
[Authorize]
[HttpGet]
public ActionResult GetSelectedClientID()
{
selectedClientId = HelperMethods.GetClientId();
return PartialView(selectedClientId);
}
感谢任何帮助。请注意,我是MVC的新手!
答案 0 :(得分:1)
在这种情况下你的控制器应该返回纯文本,或者你可以返回json并使用ajax调用,但以下内容应该让你继续:
[Authorize]
[HttpGet]
public ActionResult GetSelectedClientID()
{
var selectedClientId = HelperMethods.GetClientId().ToString();
return Content(selectedClientId);
}
现在可以查看返回的字符串值:
@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>
}
答案 1 :(得分:0)
如果我理解正确,您希望根据值有条件地隐藏部分视图。
您可以在主要操作中使用ViewData
字典:
[Authorize]
[HttpGet]
public ActionResult MyAction()
{
ViewData["selectedClientId"] = HelperMethods.GetClientId();
return View();
}
在您的视图中以这种方式检查其价值:
@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>
}