7

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?

1
  • 2
    I think you're looking for Html.RenderAction or whatever it is in Razor... Commented Sep 1, 2011 at 18:42

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

Comments

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

or

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

(without semicolon)

2 Comments

The Html.Action("RssFeed", "RSSFeed") works but how should I get the ViewData from the RSSController?
Whatever you set in ViewData in your action, is reflected in a Model variable in the view, even if it's partial. I mean, it's independent from the host page and the host page's model, it has "a life of its own".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.