如何在 foreach 循环中迭代 ViewBag 名称?

How to iterate ViewBag names inside foreach loop?

我设法在 SO 中找到关于遍历 ViewBag 对象的问题,但我的问题是如何使用迭代在其中命名 ViewBag

但从头开始。在我的 Razor View 中,我有一个代码,它根据传递的 ViewBag:

触发脚本
@if (ViewBag.TermsChecked == "false")
{
    <script type="text/javascript">
        document.getElementById("termschecked").style.border = "2px solid #E24941";
    </script>
}
else if (ViewBag.TermsChecked == "true")
{
    <script type="text/javascript">
        document.getElementById("termschecked").style.border = "unset";
    </script>
}

@if (ViewBag.LoginCaptchaChecked == "false")
{
    <script type="text/javascript">
        document.getElementById("logincaptchachecked").style.border = "2px solid #E24941";
    </script>
}
else if (ViewBag.LoginCaptchaChecked == "true")
{
    <script type="text/javascript">
        document.getElementById("logincaptchachecked").style.border = "unset";
    </script>
}

@if (ViewBag.RegisterCaptchaChecked == "false")
{
    <script type="text/javascript">
        document.getElementById("registercaptchachecked").style.border = "2px solid #E24941";
    </script>
}
else if (ViewBag.RegisterCaptchaChecked == "true")
{
    <script type="text/javascript">
        document.getElementById("registercaptchachecked").style.border = "unset";
    </script>
}

@if (ViewBag.CaptchaChecked == "false")
{
    <script type="text/javascript">
        document.getElementById("captchachecked").style.border = "2px solid #E24941";
    </script>
}
else if (ViewBag.CaptchaChecked == "true")
{
    <script type="text/javascript">
        document.getElementById("captchachecked").style.border = "unset";
    </script>
}

为了保留它 DRY 我想通过 ViewBag 名称创建一个循环,但我不确定如何传递 item 进入循环中的 ViewBag 名称。到目前为止,我已经尝试了两种方法,但都没有用:

@{string[] viewbags = new string[] { ViewBag.TermsChecked, ViewBag.LoginCaptchaChecked, ViewBag.RegisterCaptchaChecked, ViewBag.CaptchaChecked };}


@foreach (string item in viewbags)
{
    if (item == "false")
    {
        <script type="text/javascript">
            document.getElementById("@item.ToLower()").style.border = "2px solid #E24941";
        </script>
    }
    else if (item == "true")
    {
        <script type="text/javascript">
            document.getElementById("@item.ToLower()").style.border = "unset";
        </script>
    }
}

@{string[] viewbags = new string[] { "TermsChecked", "LoginCaptchaChecked", "RegisterCaptchaChecked", "CaptchaChecked" }}


@foreach (string item in viewbags)
{
    @if (ViewBag.item == "false")
    {
        <script type="text/javascript">
            document.getElementById("@item.ToLower()").style.border = "2px solid #E24941";
        </script>
    }
    else if (ViewBag.TermsChecked == "true")
    {
        <script type="text/javascript">
            document.getElementById("@item.ToLower()").style.border = "unset";
        </script>
    }
}

两者都没有触发任何脚本,就像他们应该做的那样。

您可以尝试像这样修改您的 Viewbag :

public IActionResult Index()
{
    List<Item> items = new List<Item>();
    items.Add(new Item { key = "LoginCaptchaChecked", value = "false" });
    items.Add(new Item { key = "RegisterCaptchaChecked", value = "true" });
    ViewBag.showHidelist = items;
    return View();
}
public class Item { 
    public string key { get; set; }

    public string value { get; set; }
}

并且在页面中您可以动态设置边框:

@foreach (var item in ViewBag.showHidelist)
{
    if (@item.value=="true")
    {
        <script type="text/javascript">
            document.getElementById("@item.key.ToLower()").style.border = "2px solid #E24941";
        </script>
    }
    else
    {
        <script type="text/javascript">
            document.getElementById("@item.key.ToLower()").style.border = "unset";
        </script>
    }

}

使用for可以解决你的问题。

代码:

@for (int i = 0; i < ViewBag.showHideList.Count(); i++)
{
    Item itm = ViewBag.showHideList[i];

    ...
}