asp.net-mvc 用于局部视图的 ASP.NET MVC 3 控制器操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7274831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:26:35  来源:igfitidea点击:

ASP.NET MVC 3 controller action for partial view

asp.net-mvcpartial-views

提问by user686924

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:

我是 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:

rss控制器:

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:

_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:

_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?

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

回答by jim tollan

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

您需要调用的是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);
        }
    }

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

然后,您需要有一个名为 的局部视图RssFeed

回答by Ofer Zelig

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

or

或者

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

(without semicolon)

(不带分号)