1

I'm currently stuyding MVC-5 ASP.NET and following this tutorial: https://www.youtube.com/watch?v=Fu9v2MIDlTA

I have successfully created my view and controller and therefore my HeLLO WORLD display. Now I'm currently in part 2 of the tutorial wherein I'm passing my data from controller to View.

Here are the codes:

Home Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication2.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ShowHome()
        {
            ViewData["CurrentTime"] = DateTime.Now.ToString();
            return View("Home");
        }
    }
}

View:

@{
    ViewBag.Title = "Home";
}
<html>

<head>
    <title>
        WELCOME TO MVC 5
    </title>
    <body>
        <div>
            This is my homepage! <br/>
            TIME: <%= ViewData["CurrentTime"]%>
        </div>
    </body>
</head>

</html>

The question is: Why is it that when I type the part: <% %>, my ide is not reading it? I mean, the speaker in the tutorial is using I think Visual Studio 2013 Ultimate. When he types <% >%, there is intellisense.

I'm using Visual Studio 2013 Express for WEB, when I type

ViewData["CurrentTime"] = DateTime.Now.ToString();

The IDE doesn't recognize it and it prints this as a string. :(

3
  • 1
    Since you're using the Razor view-engine, you're gonna need this instead: @ViewData["CurrentTime"]. Commented Jul 8, 2015 at 7:35
  • 1
    It's the OLD ASP.NET syntax. With new syntax <%= ViewData["CurrentTime"]%> has to be changed to @ViewData["CurrentTime"]. BTW I'd drop that tutorial in favor of something newer. Concepts didn't change much but everything else changed a lot... Commented Jul 8, 2015 at 7:35
  • @haim770 right, I supposed to write "ASP.NET syntax" instead of instead of "Razor", thank you Commented Jul 8, 2015 at 7:38

1 Answer 1

1

Glenn you got confused between they way on how code blocks where annotated in aspx pages and how they are used in the Razor view engine nowadays.

You can see Razor in action right at the top of your View:

@{
    ViewBag.Title = "Home";
}

And thats how you do it. The curly braces are optional, if you use a single expression, so in your case you could just write

<div>
    This is my homepage! <br/>
    TIME: @ViewData["CurrentTime"]
</div>

Scott Gu has written a pretty good article about the differences between the two systems: http://weblogs.asp.net/scottgu/introducing-razor

As a rule of thumb: If its an .aspx file, use <% %>. If it is a .cshtml or .vbhtml use @{ }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.