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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:57:44+00:00 2026-06-10T13:57:44+00:00

I am teaching myself asp .net mvc3. I have a partial view which uses

  • 0

I am teaching myself asp .net mvc3.

I have a partial view which uses various models and lookup type db. I want to keep it strongly typed and use it in multiple places but I am not sure how to implement it.
The example below should explain it better. This question might get a bit long and I appreciate your patience.

The partial view basically gives a small description of the property. A snippet of the ‘_PropertyExcerptPartial’ is below:

@model Test.ViewModels.PropertyExcerptViewModel

<div>
    <h3>@Html.DisplayFor(model => model.Property.NoOfBedrooms) bedroom @Html.DisplayFor(model => model.FurnitureType.FurnitureTypeDescription) flat to Rent </h3>
…
</div>

I want to keep this partial view strongly typed and use it in multiple places. The model that it is strongly typed to is as follows:

public class PropertyExcerptViewModel
{
    public Property Property { get; set; }
    public FurnitureType FurnitureType { get; set; }
}

The two 2 database that this model looks up is as follows:

 public class Property
 {
    public int PropertyId {get; set; }       
    ...
    public int NoOfBedrooms {get; set;}
    public int FurnishedType { get; set; }
    ...
 }

public class FurnishedType
{
    public int FurnishedTypeId { get; set; }
    public string FurnishedTypeDescription { get; set; }
}

The furnished type database is basically just a lookup table with the following data:
1 – Furnished
2 – Not Furnished
3 – Part Furnished
4 – Negotiable

I have many such lookups in that I only store an int value in the property database which can be used to look up the description. These databases are not linked to property database and the value of furniture type is read via a function GetFurnitureType(id). I pass stored int value of Property.FurnitureType as the id.

However, I encounter a problem when I try to use this partial view as I am not sure how to pass these multiple models from a view to partial view.

Say I am trying to create an ‘added property’ page. This page basically list the properties added by the logged in user. To facilitate this, I have created another function called GetAddedProperties(userId) that return the properties added by a particular user. In the ‘added property’ view, I can call a foreach function to loop through all the properties returned by GetAddedProperties and display the _PropertyExcerptPartial. Something like this:

<div>
    @foreach (var item in //Not sure what to pass here)
    {
        @Html.Partial("_PropertyExcerptPartial",item)
    }
</div>

However, I can’t use the partial view to display information as it will display the int value of furniture type stored in the property database and I am not sure how to get the corresponding FurnitureTypeDescription and pass it to the partial view from the ‘added property’ page.

Any help would be appreciated. Thanks a lot!

  • 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-10T13:57:46+00:00Added an answer on June 10, 2026 at 1:57 pm

    You should start by designing a real view model, not some class that you suffix its name with ViewModel and stuff your domain models inside. That’s not a view model.

    So think of what information you need to work with in your view and design your real view model:

    public class PropertyExcerptViewModel
    {
        public int NoOfBedrooms  { get; set; }
        public string FurnishedTypeDescription { get; set; }
    }
    

    and then adapt your partial to work with this view model:

    @model Test.ViewModels.PropertyExcerptViewModel
    <div>
        <h3>
            @Html.DisplayFor(model => model.NoOfBedrooms) 
            bedroom 
            @Html.DisplayFor(model => model.FurnitureTypeDescription) 
            flat to Rent 
        </h3>
        ...
    </div>
    

    OK, now that we have a real view model let’s see how we could populate it. So basically the main view could be strongly typed to a collection of those view models:

    @model IEnumerable<Test.ViewModels.PropertyExcerptViewModel>
    <div>
        @foreach (var item in Model)
        {
            @Html.Partial("_PropertyExcerptPartial", item)
        }
    </div>
    

    and the last bit of course is the main controller that will do all the db querying and building of the view model that will be passed to the main view:

    [Authorize]
    public ActionResult SomeAction()
    {
        // get the currently logged in username
        string user = User.Identity.Name;
    
        // query the database in order to fetch the corresponding domain entities
        IEnumerable<Property> properties = GetAddedProperties(user);
    
        // Now let's build the view model:
        IEnumerable<PropertyExcerptViewModel> vm = properties.Select(x => new PropertyExcerptViewModel
        {
            NoOfBedrooms = x.NoOfBedrooms,
            FurnishedTypeDescription = GetFurnitureType(x.FurnishedType).FurnishedTypeDescription
        });
    
        // and finally pass the view model to the view
        return View(vm);    
    }
    

    Be careful with the lazy nature of EF if that is the ORM that you are using in order not to fall into the SELECT N + 1 trap.

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

Sidebar

Related Questions

I am teaching myself asp .net mvc3. I have researched a lot but the
I am teaching myself asp .net mvc3. I want to create a user account
I have recently started teaching myself C# and Asp.net. I am trying to build
I am teaching myself Asp .net MVC 3. I have reached a stage where
I am teaching myself some Python and I have come across a problem which
I am teaching myself asp .net mvc 3. I read this tutorial: http://www.codeproject.com/Articles/148949/ASP-NET-MVC-3-the-Razor-View-Engine-and-Google-Map and
I'm teaching myself ASP.NET MVC, and am trying to figure out where best to
Teaching myself some c# and my problem is that I have a class which
I'm teaching myself VB.Net. Here is a problem I have recently come across. Say
I am teaching myself to use JavaCC in a hobby project, and have 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.