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

  • Home
  • SEARCH
  • 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 4007964
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:41:45+00:00 2026-05-20T08:41:45+00:00

I’m having trouble getting my head around sending multiple models to a view in

  • 0

I’m having trouble getting my head around sending multiple models to a view in mvc.
My problem is the following.

Using EF4 I have a table with attributes organised by category.

Couldn’t post an image 🙁
[Have a table called attributes (AttributeTitle, AttributeName, CategoryID) connected to a table called Category (CategoryTitle).]

What I want to do is be able to edit an attribute entity and have a dropdown of categories to choose from.

I tried to make a custom viewmodel

    public class AttributeViewModel
{
    public AttributeViewModel()
    {
    }

    public Attribute Attribute { get; set; }
    public IQueryable<Category> AllCategories { get; set; }
}

But it just ended up being a mess.

<div class="editor-field">
        <%: Html.DropDownList("Category", new SelectList((IEnumerable)Model.AllCategories, "CategoryID", "CategoryName")) %>
    </div>

I was getting it back to the controller…

        [HttpPost]
    public ActionResult Edit(int AttributeID, FormCollection formcollection)
    {
        var _attribute = ProfileDB.GetAttribute(AttributeID);
        int _selcategory = Convert.ToInt32(formcollection["Category"]);
        _attribute.CategoryID = (int)_selcategory;

        try
        {
            UpdateModel(_attribute); (<---Error here)
            ProfileDB.SaveChanges();

            return RedirectToAction("Index");
        }
        catch (Exception e)
        {

            return View(_attribute);
        }
    }

I’ve debugged the code and my _attribute looks correct and _attribute.CategoryID = (int)_selcategory updates the model, but then I get the error.

Somewhere here I thought that there should be a cleaner way to do this, and that if I could only send two models to the view instead of having to make a custom viewmodel.

To sum it up:
I want to edit my attribute and have a dropdown of all of the available categories.

Any help much appreciated!

  • 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-20T08:41:46+00:00Added an answer on May 20, 2026 at 8:41 am

    Solved it!

    If anyone is interested, my solution is the following.
    I created a class:

    public class AttributeEditViewModel
    {
        public AttributeEditViewModel()
        {
        }
    
        public AttributeEditViewModel(Attribute attribute, SelectList categories)
        {
            this.attribute = attribute;
            this.categories = categories;
        }
    
        public Attribute attribute { get; set; }
        public SelectList categories { get; set; }
    }
    

    My controller:

            // GET: /Attribute/Edit/5
        public ActionResult Edit(int AttributeID)
        {
            Attribute _attribute = ProfileDB.GetAttribute(AttributeID);
    
            var _viewmodel = new AttributeEditViewModel
            {
                attribute = _attribute,
                categories = new SelectList(ProfileDB.GetAllCategories(), "CategoryID", "CategoryName", _attribute.CategoryID)
            };
    
            return View(_viewmodel);
        }
    

    Part of my view:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<KnowledgePortal.ViewModel.AttributeEditViewModel>" %>
    
    
    
    <div class="editor-field">
         <%: Html.DropDownList("Category", Model.categories, Model.attribute.CategoryID) %>
    </div>
    
    <p>
         <input type="submit" value="Save" />
    </p>
    

    And the postback:

    // POST: /Attribute/Edit/5
    [HttpPost]
    public ActionResult Edit(int AttributeID, FormCollection formcollection)
    {
        var _attribute = ProfileDB.GetAttribute(AttributeID);
        int _selcategory = Convert.ToInt32(formcollection["Category"]);
        _attribute.CategoryID = (int)_selcategory;
    
        try
        {
            ProfileDB.SaveChanges();
    
            return RedirectToAction("Index");
        }
        catch (Exception e)
        {
    
            return View(_attribute);
        }
    }
    

    Basically my problem was due to my lack of understanding what updatemodel did.
    So after “_attribute.CategoryID = _selcategory” all I had to do was savechanges, but first I tried to updatemodel which kept failing.

    Hope this helps someone else!

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.