部分视图的 ASP.NET MVC 3 控制器操作
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要调用
PartialView
而不是视图,修改后的操作如下所示:然后您需要有一个名为
RssFeed
的部分视图。You need to be calling the
PartialView
rather than the View, here's how a modified action would look:You would then need to have a partial view called
RssFeed
.或
(不带分号)
or
(without semicolon)