一个主要的 ASP.NET 上的多个部分视图

发布于 2024-10-31 15:58:55 字数 736 浏览 4 评论 0原文

尝试在 ASP.NET 的主页上放置强类型的部分视图,但似乎不起作用,这是我

对 ASP.NET 和部分视图的新代码。

控制器:

    public ActionResult VenuePartial()
    {
        ViewData["Message"] = _entities.VenuePartialList();
        return View();
    }

存储库:

    public IEnumerable<Venue> VenuePartialList()
    {
        var list = from s in _entities.Venue
                   orderby s.name ascending
                   select s;
        return list.ToList();
    }

IRepository:

    IEnumerable<Venue> VenuePartialList();

索引页:

   <%Html.RenderPartial("~/Views/Venue/VenuePartial.ascx");%>

请尽快提供任何帮助,我们将不胜感激 问候 T

trying to put a strongly typed partial view on a homepage in asp.net but it wont seem to work it, here is my code

new to asp.net and partial views.

Controller :

    public ActionResult VenuePartial()
    {
        ViewData["Message"] = _entities.VenuePartialList();
        return View();
    }

Repository :

    public IEnumerable<Venue> VenuePartialList()
    {
        var list = from s in _entities.Venue
                   orderby s.name ascending
                   select s;
        return list.ToList();
    }

IRepository :

    IEnumerable<Venue> VenuePartialList();

Index Page :

   <%Html.RenderPartial("~/Views/Venue/VenuePartial.ascx");%>

Any help would be grateful asap please
regards T

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

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

发布评论

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

评论(1

金兰素衣 2024-11-07 15:58:55

也许您需要将模型传递给这个部分:

<% Html.RenderPartial("~/Views/Venue/VenuePartial.ascx", ViewData["Message"]); %>

顺便说一下,您使用 ViewData["Message"] 来传递模型而不是使用模型和强类型视图:

public ActionResult VenuePartial()
{
    return View(_entities.VenuePartialList());
}

然后:

<% Html.RenderPartial("~/Views/Venue/VenuePartial.ascx", Model); %>

这显然假设您的部分被强类型化为 IEnumerable。如果将其输入到单个Venue,您也可以考虑使用编辑器/显示模板。因此,在您的主视图中:

<%= Html.DisplayForModel() %>

以及相应的显示模板(~/Views/Shared/DisplayTemplates/Venue.ascx)中:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.Venue>" %>
<span>
    <%= Html.DisplayFor(x => x.SomePropertyOfVenue) %>
</span>

现在将为模型集合的每个项目呈现显示模板。

Maybe you need to pass a model to this partial:

<% Html.RenderPartial("~/Views/Venue/VenuePartial.ascx", ViewData["Message"]); %>

And by the way WTF are you using ViewData["Message"] to pass a model instead of using a model and a strongly typed view:

public ActionResult VenuePartial()
{
    return View(_entities.VenuePartialList());
}

and then:

<% Html.RenderPartial("~/Views/Venue/VenuePartial.ascx", Model); %>

This obviously assumes that your partial is strongly typed to IEnumerable<Venue>. If it is typed to a single Venue you might also consider using Editor/Display Templates. So in your main view:

<%= Html.DisplayForModel() %>

and in the corresponding display template (~/Views/Shared/DisplayTemplates/Venue.ascx):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.Venue>" %>
<span>
    <%= Html.DisplayFor(x => x.SomePropertyOfVenue) %>
</span>

and now the display template will be rendered for each item of the model collection.

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