1

I compiled my Razor Views with MvcRazorClassGenerator and like here: http://www.chrisvandesteeg.nl/2010/11/22/embedding-pre-compiled-razor-views-in-your-dll and in my view was source:

<h2>Hello!</h2>
<p>@Html.TextBox("hello", viewModel.foo)</p>

This tool generated .cs file with code:

WriteLiteral(" \r\n<h2>Hello!</h2>\r\n<p>");
Write(Html.TextBox("hello", viewModel.foo));
WriteLiteral("</p>");

But i have an error:

'System.Web.Mvc.HtmlHelper<dynamic>' does not contain a definition for 'TextBox' and no extension method 'TextBox' accepting a first argument of type 'System.Web.Mvc.HtmlHelper<dynamic>' could be found (are you missing a using directive or an assembly reference?)

Is it possible to use HtmlHelpers in .cs files like this?

tereško
  • 58,060
  • 25
  • 98
  • 150
FSou1
  • 1,861
  • 4
  • 18
  • 25
  • “are you missing a using directive or an assembly reference?” – svick Aug 28 '11 at 17:45
  • Using directive in Razor is `@using System.Web.Mvc.Html`. Assembly reference has to be in your project. But I think it would be weird if that namespace wasn't included by default. Another possibility is [web.config](http://stackoverflow.com/questions/3239006/how-to-import-a-namespace-in-razor-view-page/6723046#6723046). – svick Aug 28 '11 at 17:50
  • Yeap, i just wanted to write it. Found here: http://www.java2s.com/Open-Source/ASP.NET/Forum/openforum/OpenForum/Core/Views/Forum/Index.cs.htm. This tool didn't write all directives, and more of them u have to write urself. Thx. – FSou1 Aug 28 '11 at 17:54

1 Answers1

3

You are missing a reference to the namespace System.Web.Mvc.Html. To add it, you can use @using System.Web.Mvc.Html to your Razor file. Or you could add it to your web.config.

Community
  • 1
  • 1
svick
  • 236,525
  • 50
  • 385
  • 514