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.

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