简体   繁体   English

mvc在视图中显示网址列表

[英]mvc display list of urls in a view

MVC beginner over here and every advice/help is appreciated. 在这里的MVC初学者,每条建议/帮助都值得赞赏。 What I am trying to achieve is I have a list of url's and their anchor tags in a controller. 我想要实现的是在控制器中有一个URL及其锚标记的列表。 I want to post them to a new view. 我想将它们发布到新视图。 In that new view they will be displayed in a sorted list according to their name. 在该新视图中,它们将根据其名称显示在排序列表中。 So far, I have the name and the anchor text, and what I am planning to do is to put them in a ViewData and pass that in the new view. 到目前为止,我已经有了名称和锚文本,并且我打算做的是将它们放在ViewData中并在新视图中传递它。

This is what I have in the controller: 这是我在控制器中的功能:

Dictionary<string, string> list = ExtractURL(content);
                return View(new Website(list, "Addresses"));

The dictionary contains the url and the anchor text from the extracted url. 词典包含URL和提取的URL中的锚文本。 And in the view: 并在视图中:

   <%=ViewData["Addresses"] %>

What should I do now at the new view to populate the sorted list with the url and the anchor text? 现在,我应该在新视图中如何用url和锚文本填充排序列表? Every help is appreciated. 感谢您的帮助。 Thanks, Laziale 谢谢,Laziale

If you want that dictionary returned to your view, you are probably best to actually assign it to your ViewData: 如果您希望该词典返回到您的视图,则最好将其实际分配给您的ViewData:

ViewData["Addresses"] = list;
return View();

This takes the Dictionary object you populate (presumably) in the ExtractURL(content) method and makes it available for use in your view. 这将带您(大概)在ExtractURL(content)方法中填充的Dictionary对象,并使其可在视图中使用。

Your view would then iterate over each item in your list: 然后,您的视图将遍历列表中的每个项目:

<ul>
<% foreach(var Item in ViewData["Addresses"] as Dictionary<string, string>) {
%>
   <li>// Show my Item.Properties here</li>
<% }
%>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM