I have a partial view (ascx) to display the top x articles of an RSS feed user:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Models.Article>>" %>
<% foreach (var item in Model) { %>
<a href="<%: item.Url %>"><%: item.Title %></a><br />
<%: String.Format("{0:g}", item.Date) %><br />
<%: item.Body %>
<br />
<% } %>
In addition, I have a method that takes an RSS URL and returns an IEnumerable of type Article:
public static class ArticleFeedHelper
{
public static IEnumerable<Models.Article> GetArticles(string feedUrl)
{
}
}
In one of my views, I call the partial view as follows:
<div id="articleList" class="section">
<div class="sectionTitle">My Recent Articles</div>
<hr />
<div class="sectionBody">
<% Html.RenderPartial("ArticleList", ArticleFeedHelper.GetArticles(Model.RSSFeed)); %>
</div>
</div>
The problem is that I would like to put the error message in the main view. Just because the RSS feed cannot be repaired, it should not be so destructive that it throws the user to a full page of errors.
Decision:
Partial view and ArticleFeedHelper remain the same.
Using Dave's suggestions , the main view has been changed to:
<div id="articleList" class="section">
<div class="sectionTitle">My Recent Articles</div>
<hr />
<div class="sectionBody">
<% Html.RenderAction("ArticleList", "ArticleList", new { feedUrl = Model.RSSFeed }); %>
</div>
</div>
Partial controller added:
public class ArticleListController : Controller
{
public ActionResult Index()
{
return View();
}
[ChildActionOnly]
public ActionResult ArticleList(string feedUrl)
{
try
{
IEnumerable<Models.Article> articles = Helpers.ArticleFeedHelper.GetArticles(feedUrl);
return PartialView(articles);
}
catch (Exception ex)
{
}
}
}
And the route is added:
routes.MapRoute(
"ArticleList",
"{controller}/{action}/{feedUrl}",
new { controller = "ArticleList", action = "ArticleList", feedUrl = "" }
);