Tutorials ASP.NET Core Complete Tutorial (ShopNest)

Models and ViewModels in ASP.NET Core

Learn Models and ViewModels in ASP.NET Core in our free ASP.NET Core Complete Tutorial (ShopNest) series. Step-by-step explanations, examples, and interview tips on Toolliyo Academy.

On this page
Models and ViewModels in ASP.NET Core — ShopNest
Article 9 of 75 · Module 1: Foundations · ShopNest Job Portal
Target keyword: viewmodels asp.net core · Read time: ~27 min · .NET: 8 / 9 · Project: ShopNest Job Portal

Introduction

The Job Portal needs JobSeeker, JobListing, and Company entities — but you should not expose domain models directly to Razor. ViewModels shape data for each screen and carry validation rules.

After this article you will

  • Distinguish domain models, view models, and DTOs
  • Apply Data Annotations for validation
  • Build Job Portal view models from scratch
  • Validate models in controllers with ModelState

Prerequisites

Concept deep-dive

TypePurposeExample
Domain modelDatabase entityJobListing with EF navigation props
ViewModelOne screen / formJobApplyViewModel with resume upload
DTOAPI boundaryJobListingDto without internal fields
public class JobApplyViewModel
{
    [Required][StringLength(100)]
    public string FullName { get; set; }

    [Required][EmailAddress]
    public string Email { get; set; }

    [Range(0, 50)]
    public int YearsExperience { get; set; }

    [Required]
    public IFormFile Resume { get; set; }
}

In controller: if (!ModelState.IsValid) return View(model);. AutoMapper (Article 50) maps entity ↔ view model at scale.

Hands-on — ShopNest Job Portal

  1. Define entities: JobSeeker, Company, JobListing.
  2. Create JobSearchViewModel with filters (location, keyword, min salary).
  3. JobDetailsViewModel combines listing + company + apply form.
  4. Validate POST apply action; show field errors in view.

Common errors & best practices

  • Over-posting: bind only safe properties — use view models, not entities, on forms.
  • Missing [Required] on nullable value types — use Required or nullable reference types.

Interview questions

Q: Why not pass JobListing entity to view?
A: Over-posting, circular references, leaking internal fields; view models expose only what UI needs.

Q: Data Annotations vs FluentValidation?
A: Annotations on model; FluentValidation separate classes — better for complex rules (Article 39).

Summary

  • Domain models map to DB; view models map to screens
  • Data Annotations drive server validation
  • Job Portal demonstrates search and apply view models

Previous: Layouts, Partial Views and View Components
Next: Forms, Model Binding and Validation

FAQ

Is ViewModel mandatory?

Strongly recommended for any non-trivial form or composite page.

Can one view model serve multiple views?

Possible but prefer focused view models per use case.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior Detailed
Explain CLR & types in the context of ASP.NET Core Complete Tutorial (ShopNest).
Short answer: The CLR loads assemblies, manages memory (GC), and JIT-compiles IL to native code. Value types live on the stack or inline in objects; reference types live on the heap with GC tracking. Real-world example (…
Mid Detailed
What are common mistakes teams make with ASP.NET Core when using ASP.NET Core Complete Tutorial (ShopNest)?
Short answer: ASP.NET Core is cross-platform, uses Kestrel, middleware pipeline, and built-in DI. Requests flow: routing → middleware → endpoints → filters → action. Real-world example (ShopNest) In a ShopNest .NET servi…
Senior Detailed
How would you debug a production issue related to EF Core in a ASP.NET Core Complete Tutorial (ShopNest) application?
Short answer: EF Core maps C# entities to tables, tracks changes, and translates LINQ to SQL. Migrations version schema; Include/ThenInclude load graphs. Real-world example (ShopNest) In a ShopNest .NET service, explain…
Junior Detailed
Describe a real-world scenario where Testing mattered in a ASP.NET Core Complete Tutorial (ShopNest) project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Testing i…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

ASP.NET Core Complete Tutorial (ShopNest)
Course syllabus
Module 1: Foundations
Module 2: Entity Framework Core
Module 3: Dependency Injection & Middleware
Module 4: Authentication & Security
Module 5: Web API
Module 6: Advanced Architecture
Module 7: Testing
Module 8: Deployment & DevOps
Module 9: Real-World Projects
Module 10: Advanced Topics
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details