Access a Viewbag like an Array?

后端 未结 5 1291
余生分开走
余生分开走 2021-01-13 14:44

Imagine a view bag called

 ViewBag.Modes

this contains the following:

Simple
Advanced
Manual
Complete

Ho

5条回答
  •  北海茫月
    2021-01-13 15:35

    This does the trick for me:

    Controller:

    public ActionResult Index()
    {
        var stringArray = new string[3] { "Manual", "Semi", "Auto"};
        ViewBag.Collection = stringArray;
        return View();
    }
    

    View:

        @foreach(var str in ViewBag.Collection)
        {
            @Html.Raw(str); 
    } @for (int i = 0; i <= 2; i++ ) { @Html.Raw(ViewBag.Collection[i])
    }

    Output:

    enter image description here

    Sorry for not using your terms. I was scrachting this together from the top of my head.

提交回复
热议问题