Rashwanov
Rashwanov

Reputation: 41

@Html.Raw in MVC 4

i was trying to create a news website which display its content from several news websites

as start am trying Webclient little bit and working fine on a simple Web Form

but when get to MVC i get: The name 'read' does not exist in the current context

CODE BELOW:

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();
    }

and the News.cshtml view:

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

any tips .. even if i should try something else to get specific content from other websites ?

Upvotes: 3

Views: 3895

Answers (1)

Matt
Matt

Reputation: 3233

read is a local scoped to the controller action method. The view doesn't know about it. You have to explicitly send it to the View. You can do this with ViewBag.

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

and then in your view

@Html.Raw(ViewBag.Read)

You can also create a Model class and strongly type the view to your model, or even a List of instances of your Model class

Upvotes: 5

Related Questions