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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:53:40+00:00 2026-06-17T10:53:40+00:00

I have two models, a code model and a tag model which are linked

  • 0

I have two models, a code model and a tag model which are linked by a many to many relationship. I am trying to add a code entry that includes a possible selection of many tags using a view model (using check boxes for the tags in my view). I am getting the error:

The model item passed into the dictionary is of type ‘System.Collections.Generic.List’1[StoRed.Models.Code]’, but this dictionary requires a model item of type ‘System.Collections.Generic.IEnumerable`1[StoRed.Models.CodeTagViewModel]’.

It feels like I need to somehow convert my data to the acceptable format before trying to save it into the table but I’m new to MVC and I am having trouble finding any useful information on the internet about my specific problem. Any help would be greatly appreciated.

The code model

public class Code
{
    [Key]
    public int CodeID { get; set; }

    [Required]
    [StringLength(30)]
    public string Title { get; set; }

    [Required]
    [StringLength(150)]
    public string Description { get; set; }

    public DateTime DateAdded { get; set; }

    public DateTime LastUpdated { get; set; }

    [Required]
    [StringLength(30)]
    public string Project { get; set; }

    [Required]
    [StringLength(30)]
    public string CMS { get; set; }

    public int DotNetVersion { get; set; }

    [Required]
    [StringLength(150)]
    public string Dependencies { get; set; }

    [StringLength(30)]
    public string Author { get; set; }

    public string CodeFile { get; set; }

    [Required]
    [StringLength(100)]
    public string TFSLocation { get; set; }

    ////Creates a relationship in the DB with Tag
    //[ForeignKey("TagID")]
    public virtual ICollection<Tag> Tags { get; set; }

    ////Purely for API
    //[Required]
    public int TagID { get; set; }
}

The Tag model

public class Tag
{
    [Key]
    public int TagID { get; set; }

    [Required]
    [StringLength(30)]
    public string TagName { get; set; }

    public virtual ICollection<Code> Code { get; set; }
}

The context

public class Context : DbContext
{
    public DbSet<Code> Code { get; set; }

    public DbSet<Tag> Tags { get; set; }
}

The view model

public class CodeTagViewModel
{
    public Tag Tag { get; set; }
    public Tag TagID { get; set; }
    public List<Tag> Tags { get; set; }


    public int CodeID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime DateAdded { get; set; }
    public DateTime LastUpdated { get; set; }
    public string Project { get; set; }
    public string CMS { get; set; }
    public int DotNetVersion { get; set; }
    public string Dependencies { get; set; }
    public string Author { get; set; }
    public string CodeFile { get; set; }
    public string TFSLocation { get; set; }
}

Relevant part of the code controller

    [HttpPost]
    public ActionResult Create(CodeTagViewModel codeTagViewModel)
    {
        if (ModelState.IsValid)
        {
            Code code = new Code();
            Tag tag = new Tag();

            var codeTag = new CodeTagViewModel();

            code.Title = codeTagViewModel.Title;
            code.Description = codeTagViewModel.Description;
            code.DateAdded = codeTagViewModel.DateAdded;
            code.LastUpdated = codeTagViewModel.LastUpdated;
            code.Project = codeTagViewModel.Project;
            code.CMS = codeTagViewModel.CMS;
            code.DotNetVersion = codeTagViewModel.DotNetVersion;
            code.Dependencies = codeTagViewModel.Dependencies;
            code.Author = codeTagViewModel.Author;
            code.CodeFile = codeTagViewModel.CodeFile;
            code.TFSLocation = codeTagViewModel.TFSLocation;
            code.Tags = codeTagViewModel.Tags;

            db.Code.Add(code);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }

        return View(codeTagViewModel);
    }
  • 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-17T10:53:42+00:00Added an answer on June 17, 2026 at 10:53 am

    Your best bet is to create some kind of provider/manager/service/factory/handler – choose a name that makes most sense in terms of the job it is doing within the flow of data through your system – that is responsible for taking the ViewModel and mapping the properties of the ViewModel into an instance of the domain model before persisting the domain model to the data store, either itself or by passing the hydrated domain model to a repository layer. You can either do this manually or by using something like AutoMapper. Here’s a quick manual example:

    Create a CommandHandlers folder in your web project with the interface and dependant handler:

    public interface ICodeCommandHandler
    {
        int Save(CodeTagViewModel input);
    }
    
    public class CodeCommandHandler : ICodeCommandHandler
    {
        private IRepository<Code> repository;
    
        public CodeCommandHandler(IRepository<Code> repository)
        {
            this.repository = repository;
        }
    
        public int Save(CodeTagViewModel input)
        {
            Code code = new Code();
            Tag tag = new Tag();
            code.Title = input.Title;
            code.Description = input.Description;
            code.DateAdded = input.DateAdded;
            code.LastUpdated = input.LastUpdated;
            code.Project = input.Project;
            code.CMS = input.CMS;
            code.DotNetVersion = input.DotNetVersion;
            code.Dependencies = input.Dependencies;
            code.Author = input.Author;
            code.CodeFile = input.CodeFile;
            code.TFSLocation = input.TFSLocation;
            code.Tags.Add(tag);
    
            return repository.Save(code);
    
        }
    }
    

    Then in your controller, inject the ICodeCommandHandler in via constructor injection, the same as you do with the repository in the CodeCommandHandler:

    private readonly ICodeCommandHandler commandHandler;
    
    public CodeController(ICodeCommandHandler commandHandler)
    {
        this.commandHandler = commandHandler;
    }
    
    [HttpPost]
    public ActionResult Create(CodeTagViewModel codeTagViewModel)
    {
        if (!ModelState.IsValid)
        {
            return View(codeTagViewModel);
        }
    
        var id = codeCommandHandler.Save(codeTagViewModel);
        // maybe do something useful with the document id after save
        return RedirectToAction("Index");  
    }
    

    To keep the Repository nice and simple, here’s how that could look:

    public interface IRepository<T>
    {
        int Save(T entity);
    }
    
    public class CodeRepository : IRepository<Code>
    {
        public int Save(Code entity)
        {
            using (var context = new Context())
            {
                context.Code.Add(entity);
                context.SaveChanges();
            }
        }
    }
    

    I’ve not gone into detail about the dependency injection side of things as that wasn’t part of the question but this should give you an idea of where to start

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

Sidebar

Related Questions

I have two models such that class Employer(models.Model): code = models.CharField(null=False,blank=False,default=) class JobTitle(models.Model): employer
I have two models in Django like follows(in pseudo code) class Medicine(db.Model): field_1 =
I have two models milestone and project, the code as below: class Project(models.Model): ID
I have two models which are linked in a has_one / belongs_to association; Computer
I have two models in my application, Transaction and Person, with a many-to-many relationship.
I have two models, Room and Image . Image is a generic model that
I have two models: OuterModel and InnerModel. There is a one to many relationship
I have two models, Article and Post that both inherit from a base model
I have two models, say, Question and Topic . I am trying to add
I have two models pages and author, here is the code of model pages:

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.