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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:13:37+00:00 2026-06-17T22:13:37+00:00

I`m writing an ASP.Net MVC application with Razor. Assume that I have HomeController and

  • 0

I`m writing an ASP.Net MVC application with Razor.

Assume that I have HomeController and some views for it.

 1. View1
 2. View2
 3. View3

All this views use common _MyLayout file, which should look like this:

When the links are clicked, the views are rendered by RenderBody() method.
Each view is strongly typed: it requires its own Model.

Everything was fine untill I decided to add special Model to _MyLayout view.

But now I get error

The model item passed into the dictionary is of type 'TestUp.Models.UserModels.PendingTestsModel', but this dictionary requires a model item of type 'TestUp.Models.UserModels.UserNavigationModel'.

Here is controllers code

public ActionResult View1()
    {
        ModelForView1 model = new ModelForView1();
        return View(model);
    }
public ActionResult View2()
    {
        ModelForView2 model = new ModelForView2();
        return View(model);
    }
public ActionResult View3()
    {
        ModelForView3 model = new ModelForView3();
        return View(model);
    }

Shortly speaking if layout view doesn`t require model, specific method for View is invoked,
model is created, passed to view and everything is ok. But now layout requires model as well so it crashes.

The question is: how do I elegantly resolve this problem?

Desired workflow is:

  1. View1 is requested
  2. Method in controller for this view is called, model instance created, passed to view
  3. Some method for layout is called, model for layout created, passed to layout.

Is it possible to make things work somehow like this?

Thanks.

  • 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-17T22:13:41+00:00Added an answer on June 17, 2026 at 10:13 pm

    Create a base model type and have your specific view models extend it. This base model can have a property of type UserNavigationModel. The layout can accept the base model and use the new property as the model for the navigation menu.

    public abstract class ModelBase
    {
        public UserNavigationModel NavigationModel { get; set; }
    }
    
    public class ModelForView1 : ModelBase { ... }
    public class ModelForView2 : ModelBase { ... }
    public class ModelForView3 : ModelBase { ... }
    

    View1:

    @model ModelForView1
    

    Layout:

    @model ModelBase
    @* use Model.NavigationModel for nav bar *@

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

Sidebar

Related Questions

I am writing a web application on Asp.Net MVC 3. Assume that we have
I have an ASP.NET MVC application, with some RESTful services that I'm trying to
I am writing an asp.net MVC app that drives an IPhone application. I want
I am writing an application (Asp.Net MVC) that contains a lot of reusable components.
I am writing an application in asp.NET, I use the MVC 3 model(views engine
I have an application in Asp.net MVC where at some point I would like
I am writing a Asp.net MVC 2 application that uses Forms Authentication and currently
I am writing an ASP.NET MVC 3 application and I have a number of
An ASP.Net MVC 3 application I've just started writing needs to have authentication for
I'm writing an ASP.Net MVC application using Jquery to post some data to my

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.