This project is read-only.

@model Type support for .cshtml

Jan 10, 2011 at 3:50 PM

One thing I've started doing recently is writing all my Razor templates in a .cshtml file so that I can get full Intellisense support and Syntax Highlighting. However, one problem with this is lack of @Model.Whatever support.

I've added @model ModelType support to the default parser but since you pass the model type in as part of the parameters it doesn't really make sense to use it to override the passed in model (which is what i'm currently doing). I'm thinking the best way to proceed is to actually ignore @model declaration and skip over it when parsing but allow it so that Intellisense works.


Jan 11, 2011 at 11:41 PM

I'm not convinced that the Razor view engine actually does any parsing of the @inherits or @model lines.  I'd have to check but I would hazard a guess that the view engine reads forward into the file and picks out those declarations and only considers the remainder of the file as the content to parse.  Those initial lines would configure the Razor host to use either a specific model (@model), or a specific base type (@inherits).

My only concern with how the project is evolving is that we'll actually end up with Razor itself, instead of the simplistic template engine we set out to build. We could add some ParseFrom and CompileFrom methods which could read from a file stream, that way they can take advantage of the declarative syntax for @models, otherwise its still got to be done in code.

Does that make sense?

Jan 12, 2011 at 12:24 AM
Edited Jan 12, 2011 at 12:37 AM

Actually the @model declaration and @inherits is parsed by the Razor Engine in MVC. Not the base parser itself. The only reason I want to add it is just for Intellisense support when editing views.

My version of the parser just skips over it unlike the real MVC parser and it's *only* used for .cshtml in design mode files.

I don't know...I think it's still the simplistic engine, I don't see why we can't add features for editing support. I don't know...It's just annoying to edit razor files without Intellisense. :)


Edit: We could also possibly add support for model checking to make sure you're passing in the right model type if you use @model...i don't know...too many thoughts! :)

Apr 5, 2011 at 11:03 PM

Sound great! I also tried to use intellisense and a way to ignore the "@model" line is exactly what I've been looking for.

Unfortunately, the following code doesn't seem to work (yet):

			string template = "@model MyModel\n Hallo @Model.Name";
			string result = Razor.Parse(template, new MyModel{ Name="World" });

Have you committed your changes yet?

Apr 6, 2011 at 3:17 PM

It was uploaded as a patch until we figure out how to best integrate it.