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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:26:02+00:00 2026-05-31T10:26:02+00:00

Following on from this question MVC3 – Should I design my Model to be

  • 0

Following on from this question

MVC3 – Should I design my Model to be tightly coupled to my View?

about how it is recommended to use a view model for your views and have the Controller populate the view model, I have been trying out Ninject.MVC and used some examples for the repository pattern to inject the required repository for a controller.

Like this

    public RecipesController(IRepository<Member> memberRepository, IRepository<Course> courseRepository, IRepository<Cuisine> cuisineRepository, IRepository<Recipe> recipeRepository) {
        this.memberRepository = memberRepository;
        this.courseRepository = courseRepository;
        this.cuisineRepository = cuisineRepository;
        this.recipeRepository = recipeRepository;
    }

Then I used MVC Scaffolding to see what the actions looked like

    public ActionResult Create() {
        ViewBag.PossibleCuisines = cuisineRepository.All;
        ViewBag.PossibleMembers = memberRepository.All;
        ViewBag.PossibleCourses = courseRepository.All;
        return View();
    }

    [HttpPost]
    public ActionResult Create(Recipe recipe) {
        if (ModelState.IsValid) {
            recipeRepository.InsertOrUpdate(recipe);
            recipeRepository.Save();
            return RedirectToAction("Index");
        } else {
            ViewBag.PossibleMembers = memberRepository.All;
            ViewBag.PossibleCourses = courseRepository.All;
            ViewBag.PossibleCuisines = cuisineRepository.All;
            return View();
        }
    }

I am having a hard time understanding how to approach the controller actions by using a view model.

Say I have a RecipeViewModel like this:

    public class RecipeViewModel {
    public Recipe Recipe { get; set; }
    public SelectList AuthorList { get; set; }
    public SelectList CourseList { get; set; }
    public SelectList CuisineList { get; set; }

    public RecipeViewModel(Recipe recipe) {
        Recipe = recipe;
    }
}

and this is the model my view would use. I presume the Create() GET action would first create this view model and would have to create a new Recipe object to pass to the ViewModel’s constructor? and the select lists can be populated by using the relevant repository such as cuisineRepository.All (but this seems like it would be duplicated in each action) and then the view model is passed to the view.

How though does ModelState.IsValid in the Create() POST action work with regards to this view model?

By doing this my controller now expects a RecipeViewModel object which itself needs a Recipe object.

Should it use interfaces for these too and have Ninject handle the rest? is this advisable?

  • 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-05-31T10:26:04+00:00Added an answer on May 31, 2026 at 10:26 am

    First of all the RecipeViewModel shouldn’t contain the Recipe object but in containing members such as:

    public class RecipeViewModel {
       public RecipeViewModel(IRepository<Course> courseRepository, IRepository<Cuisine> cuisineRepository, IRepository<Recipe> recipeRepository){
           this.courseRepository = courseRepository;
           this.cuisineRepository = cuisineRepository;
           this.recipeRepository = recipeRepository;
       }
       public string RecipeName { get; set; }
       public IList<IngredientsViewModel> Ingredients { get; set;}
       public SelectList AuthorList { get; set; }
       public SelectList CourseList { get; set; }
       public SelectList CuisineList { get; set; }
    
       public static RecipeViewModel Build()
       {
            //Build up Select Lists here and return View model.
       }
    
    }
    

    Then Validation attributes go on the ViewModel to make ModelState.IsValid work:

    public class RecipeViewModel {
       [Required]
       public string RecipeName { get; set; }
       public IList<IngredientsViewModel> Ingredients { get; set;}
       public SelectList AuthorList { get; set; }
       public SelectList CourseList { get; set; }
       public SelectList CuisineList { get; set; }
    }
    

    Personally I would then refactor your controller to decouple the data layer from the view.

    public RecipesController(IRecipeService recipeService) {
        this.recipeService  = recipeService;
    }
    

    Then the rest of the controller looks like:

    public ActionResult Create() {
        var recipeViewModel = new RecipeViewModel();
        recipeViewModel.Build():
        ViewBag.PossibleCuisines = recipeViewModel.CuisineList;
        ViewBag.PossibleMembers = recipeViewModel.AuthorsList;
        ViewBag.PossibleCourses = recipeViewModel.CourceList;
        return View();
    }
    
    [HttpPost]
    public ActionResult Create(RecipeViewModel recipe) {
        if (ModelState.IsValid) {
            _recipeService.Save(recipe)
            return RedirectToAction("Index");
        } else {
            return View();
        }
    }
    

    The IRecipeService then does the mapping from the view model to the domain model (in this case RecipeViewModel to Recipe) and then persist the domain model.

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

Sidebar

Related Questions

Following on from this question what would be the best way to write a
Following up from this question: How can I unlock a file that is locked
Following on from this question , I now want to know how to stop
Following on from this question I now have code that can attach to a
Following on from this question, what would be the best way to represent a
Following on from this question , I am interested in finding out how you
following on from this question (Developing to an interface with TDD), I'm still having
Following on from this question...I'm trying to unit test the following scenario: I have
Following on from this question... What to do when you’ve really screwed up the
Following the CSS style trick from this question I was able to create 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.