简体   繁体   English

在ASP.NET MVC中显示用户控件

[英]Displaying a user control in ASP.NET MVC

This seemed easier in Web Forms; 在Web窗体中,这似乎更容易。 I'd have a user control, with the logic in the code-behind, and I could just drop it on a page. 我要有一个用户控件,其代码中包含逻辑,然后可以将其放在页面上。

But apparently code-behinds are a no-no in MVC, and the logic is in controllers. 但是显然,代码隐藏在MVC中是不行的,逻辑在控制器中。 I'm a little confused about how logic for a user control is "wired up". 我对如何“连接”用户控件的逻辑有些困惑。

I want to display an RSS Feed user control. 我想显示一个RSS Feed用户控件。 So I have a page: 所以我有一个页面:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <p>
        <% Html.RenderPartial("RssFeed"); %>
    </p>
</asp:Content>

I have my user control: 我有用户控制权:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewPage<SyndicationFeed>" %>
<%@ Import Namespace="System.ServiceModel.Syndication" %>

<p>
    <%: ViewData.Model.Title.Text %>
</p>

<div>
    <% foreach (var item in ViewData.Model.Items)
    {
       string url = item.Links[0].Uri.OriginalString;
    %>
    <p><a href='<%= url %>'><b><%= item.Title.Text %></b></a></p>

    <% } %>
</div>

And I have this code that I need to run to get the rss data: 我有以下代码需要运行以获取rss数据:

using (XmlReader reader = XmlReader.Create(ConfigurationManager.AppSettings["RssFeed"]))
{
    SyndicationFeed rssData = SyndicationFeed.Load(reader);

    return View(rssData);
}

But where does it go? 但是它去哪儿了? In the controller for the page that contains the control? 在控制器中是否包含该页面的控件?

Based on my understanding.... Yes, it can go in the controller in the page that contains the control. 根据我的理解...。是的,它可以进入包含控件的页面的控制器中。 The partial can use the parent's ViewModel 部分可以使用父级的ViewModel

See here (the bit on Partial Views) 看到这里(部分视图上的位)

http://msdn.microsoft.com/en-us/library/dd410123.aspx http://msdn.microsoft.com/en-us/library/dd410123.aspx

EDIT To include your RSS feed If my controller were named RssController and it had a ViewResult method named RssFeed, I'd include the following on the .Master. 编辑以包括您的RSS feed如果我的控制器名为RssController且它具有名为RssFeed的ViewResult方法,则应在.Master上包括以下内容。

This causes the RssController to get invoked and return the view for your partial (assuming that's what View(rssData) sent back. 这将导致调用RssController并返回部分视图(假设这就是View(rssData)发送回的视图)。

In ASP.NET MVC 2+, you can use Html.RenderAction to render an action's result inline. 在ASP.NET MVC 2+中,可以使用Html.RenderAction内联呈现操作的结果。

Giving the action a [ChildActionOnly] attribute will prevent users from navigating to the action normally, making it usable only in this kind of "child" context. 为操作指定[ChildActionOnly]属性将阻止用户正常导航到该操作,从而使其只能在这种“子”上下文中使用。

To sum it up, this is how you can do it 总结起来,这就是你可以做到的

[HttpGet, ChildActionOnly]
// put this in RssController
public ActionResult RssFeed()
{
    // prepare the model
    SyndicationFeed rssData;
    using (XmlReader reader = XmlReader.Create(ConfigurationManager.AppSettings["RssFeed"])) 
    {
        rssData = SyndicationFeed.Load(reader);
    }

    return PartialView(rssData);
}

Usage: 用法:

<% Html.RenderAction("RssFeed", "Rss"); %>

And put a check into your ASCX if the Model you are sending in is null and if so, skip everything so it won't render anything. 如果要发送的模型为null,请在ASCX中进行检查,如果为null,请跳过所有内容以免其呈现任何内容。 If you leave it like you have it now, it will end in an exception if the Model sent in is null. 如果您现在就保留它,则如果发送的模型为null,则它将以异常结束。

I had a vaguely similar problem (using Razor view engine) which I solved by putting the code into the page since there wasn't anywhere else I wanted to put it. 我有一个类似的问题(使用Razor视图引擎),我将代码放入页面中解决了,因为我没有其他地方想放它了。 Mine actually related to rendering a menu. 我的实际上与呈现菜单有关。

The trick is to use the @functions { ... } construct. 诀窍是使用@functions { ... }构造。

Some pertinent info about the layout stuff here: http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 有关布局的一些相关信息, 请参见http : //weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx

Kind of like this in your case (not exactly right; it's late and I'm not on my dev box, but it hopefully gives the idea). 在您的情况下有点像这样(不完全正确;已经晚了,我不在开发人员名单中,但希望能给出想法)。 That way you don't need any RSS controller at all if you are only doing this small piece of RSS related stuff. 这样,如果您仅做与RSS相关的一小部分,则根本不需要任何RSS控制器。

@foreach (var item in RssFeed().Items)
{
    <p><a href='@item.Links[0].Uri.OriginalString'><b>@item.Title.Text</b></a></p>
} 



@functions {
        public SyndicationFeed RssFeed()
          {
             using (XmlReader reader = XmlReader.Create(ConfigurationManager.AppSettings["RssFeed"]))
             {
                  return SyndicationFeed.Load(reader);
             }
          }
        }

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

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