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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:14:01+00:00 2026-05-21T09:14:01+00:00

I have two classes Item and Category. Items may belong to a single category.

  • 0

I have two classes Item and Category. Items may belong to a single category. In the implementation I am using NHibernate and Item has a Category property (of type Category). Both classes are entities.

I wonder how to make it possible in a view for editing an item to be able to choose a category for example from a list or drop-down list. There are HTML helpers like Html.DropDownListFor but I don’t know how to make it work when I need to select an object (NHibernate doesn’t make CategoryId for Item accessible). Could anybody help me with the problem?

Thanks in advance
Lukasz

  • 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-21T09:14:01+00:00Added an answer on May 21, 2026 at 9:14 am

    You should be using view models in all cases. So no matter how your NHibernate models look like you are trying to display a drop down list in the view. So as always you start by defining a view model that will hold the necessary information to be used by this view:

    public class CategoryViewModel
    {
        public int SelectedItemId { get; set; }
        public IEnumerable<SelectListItem> Items { get; set; }
    }
    

    and then you would have a controller action which will fetch the model from a repository and map it to this view model:

    public ActionResult Index()
    {
        Category category = ...
        // Example using AutoMapper
        CategoryViewModel viewModel = Mapper.Map<Category, CategoryViewModel>(category);
        return View(viewModel);
    }
    

    and in the view simply:

    @Html.DropDownListFor(
        x => x.SelectedItemId,
        new SelectList(Model.Items, "Value", "Text")
    )
    

    If you don’t use view models you will be struggling to adapt your existing models to situations for which they weren’t meant to be.

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

Sidebar

Related Questions

I have two PHP model classes named Category and Item. A Category may have
I have two classes Order and Items I want a method like this class
I'm using play-1.2.3. I have two classes some kind of these: LabelItem: @Entity @Table(name=T_LABEL_ITEM)
I have two classes: Action class, that has a method for executing VBScript files,
I have two classes, Foo and Bar. Each Foo has a name and a
I have an element that has two classes at all times: <p class=general_description laptop>
I want to save images in android using SharedPreference. I have two activity classes,
i have this two classes: Item<T> : BusinessBase<T> where T : Item<T> { public
I have two C# classes, say Container and Item , and a unidirectional association:
I have the following two classes: (1) IViewModelManager (2) WorkspaceViewModel IViewModelManager has an ObservableCollection.

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.