问题描述
我是新来的MVC,我不知道如何正确使用部分景色。我试图显示RSS在我的MVC应用程序博客网站供稿。我使用的剃须刀,我有以下结构:
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的:
HomeController:
namespace MvcApp.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
}
}
RssController:
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:
_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?
推荐答案
您必须调用 PartialView
而非视图,这里有一个修改后的动作是什么样子:
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);
}
}
您会那么就需要有一个叫做局部视图的RSSFeed
。
You would then need to have a partial view called RssFeed
.
这篇关于对于局部视图ASP.NET MVC 3控制器操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!