ASP.NET MVC 将多个数据实例从控制器传递到视图

发布于 2024-09-25 14:24:43 字数 1230 浏览 2 评论 0原文

我正在尝试构建 ASP.NET MVC 2 应用程序。我想将数据从控制器传递到视图。当它只是一个单一的数据结构时我能够通过它。在控制器中:

 private ArticlesDBEntities _db = new ArticlesDBEntities();

    public ActionResult Articles()
    {
        return View(_db.ArticleSet.ToList());
    }

在视图中,我像这样迭代列表:(

<div id="demo1"> 
<% foreach (var item in Model) { %>
    <ul> 
        <li id="<%= Html.Encode(item.Id) %>"> 
            <a href="#"><%= Html.Encode(item.Title) %></a> 
            <ul> 
                <li id="phtml_2"> 
                    <a href="#">Child node 1</a> 
                </li> 
                <li id="phtml_3"> 
                    <a href="#">Child node 2</a> 
                </li> 
            </ul> 
        </li> 
    </ul> 
    <% } %>
</div> 

子节点现在出于测试原因,没有真正的角色)

但是,我现在想要处理用户尝试时的场景访问 Home/Articles/Id,不仅传递文章列表(用于填充 jsTree),还传递文章本身,这样我也可以显示它。但是,当我尝试像这样创建 ViewData 对象时:

    public ActionResult Articles()
    {
        ViewData["articlesList"] = _db.ArticleSet.ToList();
        return View();
    }

我无法找到如何在视图中迭代它。

I'm trying to build an ASP.NET MVC 2 application. I want to pass data to a view from a controller. I am able to pass it when it is only a single data structure. In the controller:

 private ArticlesDBEntities _db = new ArticlesDBEntities();

    public ActionResult Articles()
    {
        return View(_db.ArticleSet.ToList());
    }

and in the view, I iterated over the list like so:

<div id="demo1"> 
<% foreach (var item in Model) { %>
    <ul> 
        <li id="<%= Html.Encode(item.Id) %>"> 
            <a href="#"><%= Html.Encode(item.Title) %></a> 
            <ul> 
                <li id="phtml_2"> 
                    <a href="#">Child node 1</a> 
                </li> 
                <li id="phtml_3"> 
                    <a href="#">Child node 2</a> 
                </li> 
            </ul> 
        </li> 
    </ul> 
    <% } %>
</div> 

(the child nodes are for testing reasons right now, don't have a real role)

However, I now want to handle a scenario when a user tries to access Home/Articles/Id, and not only pass the article list (used for populating a jsTree), but also the Article itself, so I can show it as well. However, when I tried creating a ViewData object like so:

    public ActionResult Articles()
    {
        ViewData["articlesList"] = _db.ArticleSet.ToList();
        return View();
    }

I was unable to find out how to iterate over it in the view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Spring初心 2024-10-02 14:24:43

就传递多个数据项而言,您可以使用视图模型(首选方式)或通过视图数据来完成。如果你想通过视图模型传递它,你可以做类似的事情

public class ArticlVM
{
   public Article Myarticle{get;set;}
   public IEnumerable<ArticleSet> Artcileset{get; set;}
}

你可以填充这个视图模型并将其传递给

视图中的视图你可以访问它就像

<%=Model.Article.articlName%> 
<%=Model.Article.articlDescription%> 
<%foreach(var m in Model.Articleset){%>
   <label><%=m.Property1%></label>
<%}%>

迭代ViewData[“key”]你必须将其转换为相应的对象,例如

<%=foreach(var m in (ArticleSet)ViewData["Articles"]){%>

<label><%=m.Property1%></label>
<%}%>

as far as passing multiple data items is concerned u can do it using view models (preferred way) or by viewdata. if u want to pass it through View model u can do something like

public class ArticlVM
{
   public Article Myarticle{get;set;}
   public IEnumerable<ArticleSet> Artcileset{get; set;}
}

u can populate this view model and pass it to view

in view u can access it like

<%=Model.Article.articlName%> 
<%=Model.Article.articlDescription%> 
<%foreach(var m in Model.Articleset){%>
   <label><%=m.Property1%></label>
<%}%>

to iterate over ViewData["key"] u have to cast it to corresponding object like

<%=foreach(var m in (ArticleSet)ViewData["Articles"]){%>

<label><%=m.Property1%></label>
<%}%>
Smile简单爱 2024-10-02 14:24:43

在您的视图中,您应该能够执行

<% foreach(var item in ViewData["articlesList"]) %>

另一种方法:创建一个新的视图模型类,该类将同时保存文章和列表:

class ArticleViewModel {
   public Article Article { get;set;}
   public IEnumerable<Article> Articles { get;set;}
}

并将其传递到您的视图中。然后您可以通过 Model 属性访问它,并且您将获得强类型。

In your View you should be able to do

<% foreach(var item in ViewData["articlesList"]) %>

An alternative approach would be to create a new view model class that would hold both the article and the list:

class ArticleViewModel {
   public Article Article { get;set;}
   public IEnumerable<Article> Articles { get;set;}
}

and pass that into your view. Then you can access it through the Model property and you will get strong typing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文