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 8174087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:21:44+00:00 2026-06-06T22:21:44+00:00

Here is my problem step by step, 1- I do ajax get request from

  • 0

Here is my problem step by step,

1- I do ajax get request from my external javascript file as following,

$.ajax({
    type: "GET",
    dataType: 'json',
    url: '/Company/GetCompanies',
    error: function (xhr, status, error) {
        alert('error');
    return false;
    },
    success: function (response) {
        alert('success');
    return false;
    }
});

2 – This is the action method that receives the request

public ActionResult GetCompanies()
{
    var model = new CompanyIndex();
    model.FillCompanies();
    return Json(model ,JsonRequestBehavior.AllowGet);   
}

3 – CompanyIndex is a ViewModel that is created in my action method above.

public class CompanyIndex
{
    public IList<Company> Companies { get; set; }

    public void FillCompanies()
    {
        /* Part that is not working */
        UnitOfWork unitOfWork = new UnitOfWork();
        Companies = unitOfWork.CompanyRepository
             .Get(orderBy: q => q.OrderBy(u => u.CompanyName)).ToList();
        unitOfWork.Dispose();
        /****************************/

        /* This works fine */
         var companyList = unitOfWork.CompanyRepository
             .Get(orderBy: q => q.OrderBy(u => u.CompanyName)).ToList();
        Companies = companyList.Select(x => new { name = x.CompanyName }).ToList()
            .Select(x => new Company
            {
                CompanyName = x.name

            }).ToArray<Company>();
        /********************/
    }
}

As you can see I fetch company entities from the database using my repository and Get method that I also share below. The problem is when I fetch them from database, the type of the model that is retrieved is System.Data.Entity.DynamicProxies.Company_someRandomStringHere. In this case Ajax get request does not succeed and alerts error message.

However, if I first fetch from db and create Company objects in ViewModel and assign them to my List, ajax works fine, and ajax get request returns success. And type of Company entities when I create objects at ViewModel is CompanyManagement.Models.Company as it should be.

Just in case you need it this is the Get method I am using to fetch data from db.

public virtual IEnumerable<TEntity> Get(
     Expression<Func<TEntity, bool>> filter = null,
     Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
     string includeProperties = "")
{
    IQueryable<TEntity> query = dbSet;

    if (filter != null)
        query = query.Where(filter);

    foreach (var includeProperty in includeProperties.Split
       (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        query = query.Include(includeProperty);

        if (orderBy != null)
            return orderBy(query).ToList();
        else
            return query.ToList();
}

I don’t want to manually create new Company objects. Any ideas are really appreciated. Thanks in advance.

  • 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-06-06T22:21:45+00:00Added an answer on June 6, 2026 at 10:21 pm

    Found the problem. It seems the respond of Ajax request needs to be serializable. To do that, I canceled dynamic proxies with,

    context.ContextOptions.ProxyCreationEnabled = false;
    

    As a result my Get function now returns CompanyManagement.Models.Company entities instead of System.Data.Entity.DynamicProxies.Company.

    Edit:

    Use viewmodels to populate your views, do not return entities keeping your business logic to client side. This is bad practice and dangerous in some cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MFC-driven dialog-based application created with MSVS2005. Here is my problem step
here is my problem I have the following array (for example) string[] arr =
Maddening problem here. When my page loads: <body onload=getClientDateTime();> It runs this function: document.getElementById('ClientDateTime').value=hello
date here my problem: String datetime = 2012-03-24 23:20:51; I know that that string
Super weird problem here. I'm having trouble getting jQuery to bind any selectors except
What is the problem here? (Besides having redundant code). $.getJSON works as expected. However
I have a problem here. My Zend_Forms do not render in view script. Via
I have a problem here I can't solve. I have a database of houses
enter code here My problem is this: in this database the junction table contains
jsFiddle of the problem here. When I have an image with align=left inside a

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.