Tri*_*ete 10 model-view-controller asp.net-mvc
我是MVC框架的新手,想知道如何将RSS数据从控制器传递到视图.我知道有必要转换为某种类型的IEnumerable列表.我已经看到了创建匿名类型的一些示例,但无法弄清楚如何将RSS源转换为通用列表并将其传递给视图.
我不希望它被强类型,因为会有多个RSS源的调用.
有什么建议.
Mat*_*hew 10
我一直在玩一种在MVC中做WebParts的方法,它基本上是包含在webPart容器中的UserControls.我的一个测试用户控件是一个Rss Feed控件.我在Futures dll中使用RenderAction HtmlHelper扩展来显示它,以便调用控制器动作.我使用SyndicationFeed类来完成大部分工作
using (XmlReader reader = XmlReader.Create(feed))
{
SyndicationFeed rssData = SyndicationFeed.Load(reader);
return View(rssData);
}
Run Code Online (Sandbox Code Playgroud)
下面是控制器和UserControl代码:
Controller代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Security;
using System.IO;
namespace MvcWidgets.Controllers
{
public class RssWidgetController : Controller
{
public ActionResult Index(string feed)
{
string errorString = "";
try
{
if (String.IsNullOrEmpty(feed))
{
throw new ArgumentNullException("feed");
}
**using (XmlReader reader = XmlReader.Create(feed))
{
SyndicationFeed rssData = SyndicationFeed.Load(reader);
return View(rssData);
}**
}
catch (ArgumentNullException)
{
errorString = "No url for Rss feed specified.";
}
catch (SecurityException)
{
errorString = "You do not have permission to access the specified Rss feed.";
}
catch (FileNotFoundException)
{
errorString = "The Rss feed was not found.";
}
catch (UriFormatException)
{
errorString = "The Rss feed specified was not a valid URI.";
}
catch (Exception)
{
errorString = "An error occured accessing the RSS feed.";
}
var errorResult = new ContentResult();
errorResult.Content = errorString;
return errorResult;
}
}
}
Run Code Online (Sandbox Code Playgroud)
UserControl
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Index.ascx.cs" Inherits="MvcWidgets.Views.RssWidget.Index" %>
<div class="RssFeedTitle"><%= Html.Encode(ViewData.Model.Title.Text) %> <%= Html.Encode(ViewData.Model.LastUpdatedTime.ToString("MMM dd, yyyy hh:mm:ss") )%></div>
<div class='RssContent'>
<% foreach (var item in ViewData.Model.Items)
{
string url = item.Links[0].Uri.OriginalString;
%>
<p><a href='<%= url %>'><b> <%= item.Title.Text%></b></a>
<% if (item.Summary != null)
{%>
<br/> <%= item.Summary.Text %>
<% }
} %> </p>
</div>
Run Code Online (Sandbox Code Playgroud)
修改后面的代码有一个类型化的模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ServiceModel.Syndication;
namespace MvcWidgets.Views.RssWidget
{
public partial class Index : System.Web.Mvc.ViewUserControl<SyndicationFeed>
{
}
}
Run Code Online (Sandbox Code Playgroud)
@Matthew - 完美的解决方案 - 作为代码背后的替代方案,它往往会破坏MVC概念,您可以使用:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SyndicationFeed>" %>
<%@ Import Namespace="System.ServiceModel.Syndication" %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7002 次 |
| 最近记录: |