This project is read-only.

Do I have to use dynamic models?

Feb 20, 2013 at 12:57 PM
Edited Feb 20, 2013 at 1:18 PM
Is it necessary to use dynamic models?

I created a class "SampleModelClass" inheriting from abstract class ModelClassBase. I defined properties SampleModelClass.Firstname and SampleModelClass.Lastname.
When calling Razor.Parse<SampleModelClass>(templateContent, sampleModel); I get an exception telling me that Firstname is not found in class "ModelClassBase".

I don't understand why RazorEngine wants to use ModelClassBase? If I use a dynamic model (new {Firstname = "Foo", Lastname = "Bar";) it works fine.
public abstract class RazorTemplateModel : TemplateBase {

        /// <summary>
        /// Returns the text of the template
        /// </summary>
        /// <returns></returns>
        public abstract string GetTemplateContent();


class SampleRazorTemplateModel : RazorTemplateModel {
            private readonly string _firstname;
            private readonly string _lastname;

            public SampleRazorTemplateModel(string firstname, string lastname) {
                _firstname = firstname;
                _lastname = lastname;

            public override string GetTemplateContent() {
                const string CONTENT = @"Hello @Model.Firstname @Model.Lastname";
                return CONTENT;

            public string Firstname {
                get { return _firstname; }

            public string Lastname {
                get { return _lastname; }

SampleRazorTemplateModel templateModel = new SampleRazorTemplateModel("Foo", "Bar");
string razorTemplate = templateModel.GetTemplateContent();
return Razor.Parse(razorTemplate, templateModel.GetModel());
Jul 2, 2013 at 12:55 PM
Make your class public I had the same issue.