英文:
Loop through Viewbags with a variable name
问题
在我的ASP.NET MVC视图中,我想要循环遍历Viewbag.A
到ViewBag.Z
,但我不确定如何实现。
这是我在视图中的代码:
@{
for (char letter = 'A'; letter <= 'Z'; letter++)
{
<h1 id=@letter>@letter</h1>
if (ViewBag.@letter != null)
{
foreach (var item in ViewBag.@letter)
{
// 在这里执行一些操作
}
}
}
}
有没有办法在ViewBag
名称中使用变量,比如@letter
?
英文:
In my ASP.NET MVC View, I want to loop through Viewbag.A
to ViewBag.Z
but I'm not sure how to do this.
This is what I have in my view:
@{
for (char letter = 'A'; letter <= 'Z'; letter++)
{
<h1 id=@letter>@letter</h1>
if (ViewBag.@letter != null)
{
foreach (var item in ViewBag.@letter))
{
// do something here
}
}
}
}
Is there a way to use a variable such as @letter
in ViewBag
name?
答案1
得分: 1
你可以使用反射来枚举ViewBag
的内部ViewDataDictionary
:
@{
var dc = ViewBag.GetType()
.GetProperty("ViewData",
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance)
.GetValue(ViewBag, null);
for (char letter = 'A'; letter <= 'Z'; letter++)
{
var s = letter.ToString();
<h1 id=@letter>@letter</h1>
if (dc展开收缩 != null)
{
//在这里执行某些操作
}
}
}
英文:
You can use reflection to enumerate the internal ViewDataDictionary
of the ViewBag
:
@{
var dc = ViewBag.GetType()
.GetProperty("ViewData",
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance)
.GetValue(ViewBag, null);
for (char letter = 'A'; letter <= 'Z'; letter++)
{
var s = letter.ToString();
<h1 id=@letter>@letter</h1>
if (dc展开收缩 != null)
{
//do something here
}
}
}
答案2
得分: 1
我认为更好的方法是使用包含您的 ViewBag 项目的字典,然后通过迭代它们来获取结果,这样您的代码会看起来像这样:
public IActionResult SomeView()
{
var dic = new Dictionary<char, YourClass>();
// 在这里填充您的字典与您的类
// 其中字符将是键,类将是值
ViewBag.ClassDic = dic;
return View();
}
然后您的 Razor 脚本将类似于这样:
@{
foreach (KeyValuePair<char, YourClass> entry in ViewBag.ClassDic)
{
<h1 id=@entry.Key>@entry.Key</h1>
foreach (var item in entry.Value)
{
// 在这里执行一些操作
}
}
}
英文:
I think better way to get your result would be to have a dictionary containing your ViewBag items and iterating through them that way, so your code would look something like this:
public IActionResult SomeView()
{
var dic = new Dictionary<char,YourClass>()
//Populate your dictionary with your classes here
//Where the character will be the key and the class will be the value
ViewBag.ClassDic = dic;
return View();
}
And then your razor script will look something like this:
@{
foreach (KeyValuePair<char,YourClass> entry in ViewBag.ClassDic)
{
<h1 id=@entry.Key>@entry.Key</h1>
foreach (var item in entry.Value))
{
// do something here
}
}
}
答案3
得分: 0
试试这段代码吧
@{
for (char letter = 'A'; letter <= 'Z'; letter++)
{
var propertyName = letter.ToString();
<h1 id="@propertyName">@propertyName</h1>
if (ViewBag is IDictionary<string, object> viewBagDict && viewBagDict.ContainsKey(propertyName))
{
var items = viewBagDict[propertyName] as IEnumerable<dynamic>;
foreach (var item in items)
{
// 对每个项目执行一些操作
<p>@item</p>
}
}
}
}
英文:
Try this code man
@{
for (char letter = 'A'; letter <= 'Z'; letter++)
{
var propertyName = letter.ToString();
<h1 id="@propertyName">@propertyName</h1>
if (ViewBag is IDictionary<string, object> viewBagDict && viewBagDict.ContainsKey(propertyName))
{
var items = viewBagDict[propertyName] as IEnumerable<dynamic>;
foreach (var item in items)
{
// Do something with each item here
<p>@item</p>
}
}
}
}
答案4
得分: -2
感谢David提供的线索。我将所有数值存储在一个单独的ViewBag中。然后在视图中,我使用StartsWith()来过滤结果,如下所示:
@{
for (char letter = 'A'; letter <= 'Z'; letter++)
{
<h1 id=@letter>@letter</h1>
if (ViewBag.F != null)
{
foreach (var item in ViewBag.F)
{
if (item.Title.StartsWith(@letter.ToString()))
{
//在这里执行某些操作
}
}
}
}
}
谢谢大家。
英文:
Thank you David for the clue. I stored all the values in a single ViewBag. Then in the view, I used StartsWith() to filter the results as below
@{
for (char letter = 'A'; letter <= 'Z'; letter++)
{
<h1 id=@letter>@letter</h1>
if (ViewBag.F != null)
{
foreach (var item in ViewBag.F)
{
if (item.Title.StartsWith(@letter.ToString()))
{
//do something here
}
}
}
}
}
Thank you all.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论