MVC 4中的@ Html.Raw

时间:2013-07-22 23:00:21

标签: c# html asp.net-mvc

我正在尝试创建一个新闻网站,显示其来自多个新闻网站的内容

首先尝试使用Webclient并在一个简单的Web窗体上正常工作

但是当获得MVC时我得到:当前上下文中不存在名称“read”

以下代码:

public ActionResult News() 
    {
        var read = "";
        var msg = "";
        try
        {
            WebClient myC = new WebClient();
            read = myC.DownloadString("http://localhost:61123/Videos");

        }
        catch (Exception ex)
        {
            msg = ex.Message.ToString();
        }
        return View();
    }

和News.cshtml视图:

@ViewBag.Message.msg
@Html.Raw(read)

任何提示..即使我应该尝试其他方式从其他网站获取特定内容?

1 个答案:

答案 0 :(得分:5)

read是控制器操作方法的本地范围。该观点不了解它。您必须明确地将其发送到视图。您可以使用ViewBag执行此操作。

try
{
    WebClient myC = new WebClient();
    read = myC.DownloadString("http://localhost:61123/Videos");
    ViewBag.Read = read;
}

然后在你的视图中

@Html.Raw(ViewBag.Read)

您还可以创建Model类并强烈键入模型的视图,甚至是Model类的实例列表