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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:25:09+00:00 2026-05-22T01:25:09+00:00

Good time of a day! I have a MVC project with query in controller:

  • 0

Good time of a day!

I have a MVC project with query in controller:

var getPhotos = (from m in db.photos
    join n in db.comments on m.id equals n.photoid
    where n.ownerName == User.Identity.Name
    orderby n.id descending
    select new { 
        m.imgcrop, m.id, 
        n.commenterName, n.comment 
    }).Take(10);

How to pass this query to view model, and the model to view.

Spend all evening to find the examples, but cant. Thanks for help!

UPDATED
Full Model Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace photostorage.Models
{
    public class GlobalModel
    {
        public class PhotoViewModel
        {
            public photos Photos { get; set; }
            public profiles Profile { get; set; }
            public IQueryable<comments> Comments { get; set; }
            public IQueryable<photos> NextPrev { get; set; }
        }

        public class UserPhotoList
        {
            public IQueryable<photos> Photos { get; set; }
            public profiles Profile { get; set; }
        }

        public class UserProfileView
        {
            public IQueryable<photos> Photos { get; set; }
            public profiles Profile { get; set; }
        }

        public class GetLastComments
        {
            public IQueryable<photos> uPhoto { get; set; }
            public IQueryable<comments> uComments { get; set; }
        }
    }
}

Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using photostorage.Models;

namespace photostorage.Controllers
{
    public class HomeController : Controller
    {
        private photostorageEntities db = new photostorageEntities();

        public ActionResult Index()
        {
            if(Request.IsAuthenticated) {
                GlobalModel.GetLastComments model = new GlobalModel.GetLastComments();
                var getPhotos = (from m in db.photos
                                    join n in db.comments on m.id equals n.photoid
                                    where n.ownerName == User.Identity.Name
                                    select new { 
                                        m.imgcrop, m.id, 
                                        n.commenterName, n.comment 
                                    }).Take(10);
                return View("Index_Auth", model);
            }else{

                ViewBag.Message = "Welcome to ASP.NET MVC!";
                return View("Index");
            }
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
  • 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-22T01:25:10+00:00Added an answer on May 22, 2026 at 1:25 am

    In this case you can make a “view model” that will only be used by your view and not by the rest of your application. Something like the following:

    public class CommentsViewModel
    {
        public int MessageId { get; set; }
        public string ImageCrop { get; set; }
        public string CommenterName { get; set; }
        public string Comment { get; set; }
    }
    

    Then change your query like so:

    var getPhotos = (from m in db.photos
        join n in db.comments on m.id equals n.photoid
        where n.ownerName == User.Identity.Name
        orderby n.id descending
        select new CommentsViewModel { 
            ImageCrop = m.imgcrop, 
            MessageId = m.id, 
            CommenterName = n.commenterName, 
            Comment = n.comment 
        }).Take(10).ToList();
    

    Make your view strongly typed to the new class and pass the data to it like so:

    View("name_of_your_view", getPhotos);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day, I have a ASP.net MVC app that needs to upload files to
Thank you for your time, I have spent a good part of my day
Good time of a day. I have a problem. I need to test admob
Good day, I have a photo gallery that pulls from a json feed. The
Good day, I have a question here. I am trying to fetch image from
So I have been playing with jQuery for a good time now and I'm
What's a good web-based UI for capturing a time duration from the user. Lets
Good day, I have a gen_server process which does some long-running state-updating tasks periodically
Good day all; I have an UIImageView that I animate around a large UIView.
First off good day and thanks for taking the time to answer my question.

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.