Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7083001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:07:28+00:00 2026-05-28T07:07:28+00:00

Background .NET 4, C#, MVC3, using JsonFx to serialize and deserialize data. Base controller

  • 0

Background

.NET 4, C#, MVC3, using JsonFx to serialize and deserialize data. Base controller has been extended to intercept all requests and do the following:

  1. Get some JSON from a remote server.
  2. Run a LINQ query based on passed in keys (hero, body, footer).
  3. Return a generic model to the view.

This code works fine when running in a controller inheriting from the base controller, but when placed in the Base controller and called from an inherited controller, I get the following error:

Unable to cast object of type 'System.Linq.Expressions.ConstantExpression' to type 'System.Linq.Expressions.ParameterExpression'.

This is the offending line:

    var queryHero = heroModel.ArrayItems().Where(o => o.DisplayName == keyHero);

Question

How do I avoid getting a type error when in the base controller? It’s the same code that runs fine in the inherited controller.

Thanks for your time.

Full Code

public models.GenericPageModel GetGenericPageContent(string keyHero, string keyBody, string keyFooter)
{
    try
    {
        // get content "tables"
        var heroContent = GetJson("Page_Section_Hero_Content");
        var bodyContent = GetJson("Page_Section_Body_Content");
        var footerContent = GetJson("Page_Section_Footer_Content");

        // new reader
        var reader = new JsonReader(new DataReaderSettings(new DataContractResolverStrategy()));

        // read it
        var heroModel = reader.Query<models.PageSectionHeroContent>(heroContent);
        var bodyModel = reader.Query<models.PageSectionBodyContent>(bodyContent);
        var footerModel = reader.Query<models.PageSectionFooterContent>(footerContent);

        // run query for current page
        var queryHero = heroModel.ArrayItems().Where(o => o.DisplayName == keyHero);
        var queryBody = bodyModel.ArrayItems().Where(o => o.DisplayName == keyBody);
        var queryFooter = footerModel.ArrayItems().Where(o => o.DisplayName == keyFooter);

        // models for return
        models.PageSectionHeroContent hero;
        models.PageSectionBodyContent body;
        models.PageSectionFooterContent footer;

        // any hero content?
        if (queryHero.Any())
            hero = new models.PageSectionHeroContent { DisplayName = queryHero.FirstOrDefault().DisplayName, 
                ContentXML = queryHero.FirstOrDefault().ContentXML };
        else
            hero = new models.PageSectionHeroContent { DisplayName = "Sorry, there was an error.", 
                ContentXML = "<details><error>No data was returned.</error></details>" };

        // any body content?
        if(queryBody.Any())
            body = new models.PageSectionBodyContent { DisplayName = queryBody.FirstOrDefault().DisplayName, 
                ContentXML = queryBody.FirstOrDefault().ContentXML };
        else
            body = new models.PageSectionBodyContent { DisplayName = "Sorry, there was an error.", 
                ContentXML = "<details><error>No data was returned.</error></details>" };

        // any footer content?
        if(queryFooter.Any())
            footer = new models.PageSectionFooterContent { DisplayName = queryFooter.FirstOrDefault().DisplayName, 
                ContentXML = queryFooter.FirstOrDefault().ContentXML };
        else
            footer = new models.PageSectionFooterContent { DisplayName = "Sorry, there was an error.", 
                ContentXML = "<details><error>No data was returned.</error></details>" };

        // build generic page model
        var model = new models.GenericPageModel { PageSectionHeroContent = hero, PageSectionBodyContent = body, PageSectionFooterContent = footer };

        return model;

    }
    catch (Exception ex)
    {   
        //todo: handle, log exception
        return null;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:07:29+00:00Added an answer on May 28, 2026 at 7:07 am

    Can’t be sure what it is that is NOT initializing on time in the BaseController, but found a work-around.

    DISCLAIMER: I am on the same dev team as boynamedjib and this solution may be suited the best for our particular situation. Having said that, the solution is worth posting, we feel, as it might help others who encounter this quirk.

    Solution

    The solution was found in making use of a combination of dynamic types and ExpandoObjects that are specific to C# 4.0, as well as the IDictionary type.

     public models.GenericPageDataModel GetGenericPageContent(string keyHero, string keyBody, string keyFooter)
            {
                try
                {
                    var pageData = new List<models.GenericPageData>
                                 {
                                     new models.GenericPageData{Key = "HeroModel",Value = GetJson("pro_Page_Section_Hero_Content")},
                                     new models.GenericPageData{Key = "BodyModel",Value = GetJson("pro_Page_Section_Body_Content")},
                                     new models.GenericPageData{Key = "FooterModel",Value = GetJson("pro_Page_Section_Footer_Content")}
                                 };
    
                    var reader = new JsonReader();
    
                    var model = new models.GenericPageDataModel();
                    foreach (var p in pageData)
                    {
                        var objList = new List<ExpandoObject>();
                        dynamic output = reader.Read(p.Value);
                        foreach (var h in output)
                        {
                            dynamic obj = new ExpandoObject();
                            foreach (dynamic o in h)
                            {
                                var item = obj as IDictionary<String, object>;
                                item[o.Key] = o.Value;
                            }
                            objList.Add(obj);
                        }
                        switch (p.Key)
                        {
                            case ("HeroModel"):
                                model.HeroModel = objList.FirstOrDefault(o => o.FirstOrDefault(i => i.Key.Equals("DisplayName")).Value.Equals(keyHero));
                                break;
                            case ("BodyModel"):
                                model.BodyModel = objList.FirstOrDefault(o => o.FirstOrDefault(i => i.Key.Equals("DisplayName")).Value.Equals(keyBody));;
                                break;
                            case ("FooterModel"):
                                model.FooterModel = objList.FirstOrDefault(o => o.FirstOrDefault(i => i.Key.Equals("DisplayName")).Value.Equals(keyFooter));;
                                break;
                            default:
                                break;
                        }
                    }
                    return model;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
    

    Supporting Models

    public class GenericPageData
            {
                public string Key { get; set; }
                public string Value { get; set; }
            }
    
            public class GenericPageDataModel
            {
                public ExpandoObject HeroModel { get; set; }
                public ExpandoObject BodyModel { get; set; }
                public ExpandoObject FooterModel { get; set; }
            }
    

    Razor Usage

    @model my.project.com.Models.ContentModels.GenericPageDataModel        
    
    @{
        ViewBag.Title = "My Title";
        Layout = "~/Views/Shared/_Layout.cshtml";
    
        ViewBag.heroDisplayName = @Model.HeroModel.ToDictionary(p => p.Key).FirstOrDefault(k => k.Key.Equals("DisplayName")).Value.Value;
    }
    
    <h1> @ViewBag.heroDisplayName</h1>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First some background: VB.NET 2005 Application that accesses a MS-SQL back-end, using multiple Web
When using PNG files (made with Paint.NET) as background images on my web site,
I've got a webpage built using MVC3 and .NET 4 Validation is working great
I am currently programming a ASP.NET MVC3 application, using Entity Framework 4. There are
I have an MVC3 C#.Net web app. In my HTML View, I am using
I am trying to learn web development using Mono. Coming from ASP.NET background, I
MVC3 VB.NET application using Itextsharp. I have a section of code that generates a
I have an MVC3 C#.Net web app. In my HTML View, I am using
Coming from a .NET background I'm use to reusing string variables for storage, so
I'm coming from a .net background and want to know the accepted way of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.