Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 controller action for partial view

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?

like image 701
user686924 Avatar asked Sep 01 '11 18:09

user686924


People also ask

Can partial view have controller?

It does not require to have a controller action method to call it. Partial view data is dependent of parent model. Caching is not possible as it is tightly bound with parent view (controller action method) and parent's model.

How do I return a partial view from the action method?

How to return a partial view from controller action method? To return a Partial view from the controller action method, we can write return type as PartialViewResult and return using PartialView method.

How do I add a partial view controller?

Action function. In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.

How do I return multiple partial views from controller action?

Answers. You can only return one value from a function so you can't return multiple partials from one action method. If you are trying to return two models to one view, create a view model that contains both of the models that you want to send, and make your view's model the new ViewModel.


1 Answers

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.

like image 174
jim tollan Avatar answered Oct 13 '22 00:10

jim tollan