本文介绍了在视图中使用列表的各个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 到目前为止的代码 我已经从目录文件名在控制器中创建了一个List。 控制器 列表< string>图片= 新列表< string>(); DirectoryInfo getPic = new DirectoryInfo( @ mycomputter); FileInfo [] fType = getPic.GetFiles( *。png); foreach (FileInfo ft in fType) { Pictures.Add(ft.Name.ToString()); } ViewBag.MyList =图片; return 查看(); 查看 @ foreach( var item in ViewBag.MyList) {< tr> < td> < img src = 〜/ Content / Images / @ item style = height:150px; width:250px; /> < / td > < / tr > } 问题 在此之后我循环其内容并创建一个图片网格,这是我最初想要的。在桌面应用程序中,我将使用例如下面的一些代码 List< string> 70sPop = new List< string();> 70spop.add(abba); 70spop.add(beegees); 70spop.add(boneym); 在控制台应用程序中获取boneym我会使用 console.writeline(mcsc [2]);查看如果只想使用第3个元素。我找到了Viewbag.mylist [x],只是将自己的代码生成视图。我想只使用一个元素来生成一个字符串。在其他地方寻找信息,几乎所有解决方案都指向循环。我可以在foreach循环中使用一些条件语句,但我正在寻找是否可以调用单个元素。解决方案 我不完全确定我理解你'重新发行。我已经使用了相同的基本代码,它似乎工作正常。 我可以提供文档名称列表,我可以选择一个单独的项目显示在标签内。我的视图代码如下所示: < br /> < ul>< br /> @foreach(ViewBag.FileList中的var项目)< br /> {< br /> < li> @ item< / li>< br /> }< br /> < / ul>< br /> < br />< br /> < br />< br / > 项目编号2:< br />< br /> < img src =~/Content/Employees / @ ViewBag.FileList [2]/>< br /> The code so far I have made a List in the controller from a directories file names.ControllerList<string> Pictures = new List<string>(); DirectoryInfo getPic = new DirectoryInfo(@"mycomputter"); FileInfo[] fType = getPic.GetFiles("*.png"); foreach(FileInfo ft in fType) { Pictures.Add(ft.Name.ToString()); } ViewBag.MyList = Pictures; return View(); View@foreach (var item in ViewBag.MyList) { <tr> <td> <img src="~/Content/Images/@item" style="height:150px; width: 250px;" /> </td> </tr> }The QuestionAfter this I loop its contents and create a grid of pics, which is what I initially wanted. In Desktop Application I would use for example some code like belowList<string> 70sPop = new List<string();>70spop.add("abba");70spop.add("beegees");70spop.add("boneym");to get boneym in a console application I would useconsole.writeline(mcsc[2]); view if wanted solely to use the 3rd element. I have found Viewbag.mylist[x], just yields the code the itself to the view. I would like just use one element to produce a string. Look for information elsewhere almost all solutions pointed to a loop. I could use some conditional statements in the foreach loop but I am looking if poosible to call an individual element. 解决方案 I'm not completely sure that I understand you're issue. I've put the same basic code into place and it seems to work fine.I can provide a list of the document names and I can select an individual item to display within an tag. The code for my View is shown below:<br /><ul><br />@foreach (var item in ViewBag.FileList)<br />{<br /><li>@item</li><br />}<br /></ul><br /><br /><br /><br /><br />Item Number 2: <br /><br /><img src="~/Content/Employees/@ViewBag.FileList[2]" /><br /> 这篇关于在视图中使用列表的各个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-31 19:12