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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:31:14+00:00 2026-06-18T15:31:14+00:00

For example I have the Models: Student and Course . Student has a List<Course>

  • 0

For example I have the Models:
Student and Course.
Student has a List<Course> Courses{get;set;}

All data is already populated from the web service

I have a Student/Index which returns an ActionResult that takes a StudentViewModel
But I also have a Course/Index View which returns an ActionResult takes a CourseViewModel

That should set the foundation for my question:
Is it alright if I follow this option, which I think is very neat and simple:
Create an instance of a ViewModel in the Student/Index View

 @foreach(var course in Model.Courses)
    {
       Html.RenderPartial("path/tocourse", 
        new CourseViewModel{Course = course}) ;
    }

Or should I make StudentViewModel have a List<CourseViewModel>, so I can have:

@foreach(var courseViewModel in Model.CourseViewModels)
{
       Html.RenderPartial("path/tocourse", courseviewModel) ;
}

At first glance, the above might look better, but in the Index action (or whatever) it would require me to do the following:

 new StudentViewModel{
            CourseViewModels = courses.Select(c=>new CourseViewModel{copy stuff}).ToList()
       } 

I hate that, and also it could confuse me later on or another developer, seeing that another indirection gives me access to Courses i.e StudentViewModel.StudentModel.Courses.

So which one is better approach? Surely it can’t be that bad to create an instance of a ViewModel or a Model for that matter in a 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-06-18T15:31:16+00:00Added an answer on June 18, 2026 at 3:31 pm

    It’s perfectly OK to do this. It’s not considered Best Practice. Best Practice would be to do as little processing or instantiation as possible in your View.

    For the sake of SOC, I would re-factor this code to use Partials with Child Action Methods

    Html.Action("path/tocourse");
    

    The Child Action will instantiate and pass the Model, lightening your View.

    [ChildActionOnly]
    public ActionResult tocourse()
    {
    
    courseviewModel model = new courseviewModel();
    
        return PartialView(model);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a parent model that has many child models, for example: App.Blog =
For example if i have following tables in my database and Student and Course
I have a data model in Doctrine/symfony. I have a 'Course' which has many
For example I have 2 models: Ext.define('Order', { extend : 'Ext.data.Model', fields : [
I have a model: class Example(models.Model): unique_hash = models.CharField(max_length=32,unique=True) content = models.FileField(upload_to='source',blank=True,verbose_name=HTML Content File)
I have a model defined as below: class Example(models.Model): user = models.ForeignKey(User, null=True) other
i have task which takes a parameter and has three modes of results Example
Here's an example structure of the DB I have: In the Student view form,
For example I have 2 models that Topic and Comment . 1 Topic can
I have the following models in an app. Lesson Student Evaluation Parent Lesson and

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.