部分视图的 ASP.NET MVC 3 控制器操作

发布于 2024-12-02 16:13:37 字数 2043 浏览 1 评论 0原文

我是 MVC 新手,不明白如何正确使用部分视图。我正在尝试在我的 MVC 应用程序中显示来自博客网站的 RSS 提要。我正在使用 Razor,并且具有以下结构:

Controllers/HomeController.cs
Controllers/RssController.cs

Views/Home/Index.cshtml

Shared/_Layout.cshtml
Shared/_Rss.cshtml

HomeController:

 namespace MvcApp.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                ViewBag.Message = "Welcome to ASP.NET MVC!";

                return View();

            }

        }
    }

RssController:

namespace MvcApp.Controllers
{
    public class RSSFeedController : Controller
    {

        public ActionResult RssFeed()
        {
            string strFeed = "http://foo.wordpress.com/category/foo/feed/";

            using (XmlReader reader = XmlReader.Create(strFeed))
            {
                SyndicationFeed rssData = SyndicationFeed.Load(reader);

                return View(rssData);
            }
        }

    }
}

_Rss.cshtml:

@using System.ServiceModel.Syndication;
@using System.Text;
@using System.Xml.Linq;

<h2>RSSFeed</h2>
@foreach (var item in ViewData.Model.Items) 
{ 
string URL = item.Links[0].Uri.OriginalString; 
string Title = item.Title.Text;
StringBuilder sb = new StringBuilder();
foreach (SyndicationElementExtension extension in item.ElementExtensions)
{
    XElement ele = extension.GetObject<XElement>();
    if (ele.Name.LocalName == "encoded" && ele.Name.Namespace.ToString().Contains("content"))
    {
        sb.Append(ele.Value + "<br/>");
    }
}
Response.Write(string.Format("<p><a href=\"{0}\"><b>{1}</b></a>", URL, Title)); 
Response.Write("<br/>" + sb + "</p>"); 
}

_Layout.cshtml:

<div id="main">
    @RenderBody()
</div>
<div id="BlogContent">
    @Html.Partial("_Rss");
</div>

我的困惑是如何调用控制器操作来获取部分视图?

I'm new to MVC and I don't understand how to use partial views correctly. I'm trying to display RSS feeds from a blog site in my MVC app. I'm using Razor and I have the following structure:

Controllers/HomeController.cs
Controllers/RssController.cs

Views/Home/Index.cshtml

Shared/_Layout.cshtml
Shared/_Rss.cshtml

HomeController:

 namespace MvcApp.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                ViewBag.Message = "Welcome to ASP.NET MVC!";

                return View();

            }

        }
    }

RssController:

namespace MvcApp.Controllers
{
    public class RSSFeedController : Controller
    {

        public ActionResult RssFeed()
        {
            string strFeed = "http://foo.wordpress.com/category/foo/feed/";

            using (XmlReader reader = XmlReader.Create(strFeed))
            {
                SyndicationFeed rssData = SyndicationFeed.Load(reader);

                return View(rssData);
            }
        }

    }
}

_Rss.cshtml:

@using System.ServiceModel.Syndication;
@using System.Text;
@using System.Xml.Linq;

<h2>RSSFeed</h2>
@foreach (var item in ViewData.Model.Items) 
{ 
string URL = item.Links[0].Uri.OriginalString; 
string Title = item.Title.Text;
StringBuilder sb = new StringBuilder();
foreach (SyndicationElementExtension extension in item.ElementExtensions)
{
    XElement ele = extension.GetObject<XElement>();
    if (ele.Name.LocalName == "encoded" && ele.Name.Namespace.ToString().Contains("content"))
    {
        sb.Append(ele.Value + "<br/>");
    }
}
Response.Write(string.Format("<p><a href=\"{0}\"><b>{1}</b></a>", URL, Title)); 
Response.Write("<br/>" + sb + "</p>"); 
}

_Layout.cshtml:

<div id="main">
    @RenderBody()
</div>
<div id="BlogContent">
    @Html.Partial("_Rss");
</div>

My confusion is how do I call the controller action for getting the partial view?

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

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

发布评论

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

评论(2

记忆で 2024-12-09 16:13:37

您需要调用 PartialView 而不是视图,修改后的操作如下所示:

    public ActionResult RssFeed()
    {
        string strFeed = "http://foo.wordpress.com/category/foo/feed/";

        using (XmlReader reader = XmlReader.Create(strFeed))
        {
            SyndicationFeed rssData = SyndicationFeed.Load(reader);

            return PartialView(rssData);
        }
    }

然后您需要有一个名为 RssFeed 的部分视图。

You need to be calling the PartialView rather than the View, here's how a modified action would look:

    public ActionResult RssFeed()
    {
        string strFeed = "http://foo.wordpress.com/category/foo/feed/";

        using (XmlReader reader = XmlReader.Create(strFeed))
        {
            SyndicationFeed rssData = SyndicationFeed.Load(reader);

            return PartialView(rssData);
        }
    }

You would then need to have a partial view called RssFeed.

哥,最终变帅啦 2024-12-09 16:13:37
@Html.RenderAction("RssFeed", "RSSFeed");

@Html.Action("RssFeed", "RSSFeed")

(不带分号)

@Html.RenderAction("RssFeed", "RSSFeed");

or

@Html.Action("RssFeed", "RSSFeed")

(without semicolon)

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