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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:49:48+00:00 2026-05-19T14:49:48+00:00

Essentially what I have is an entity class (Article) and one article may belong

  • 0

Essentially what I have is an entity class (Article) and one article may belong to n categories. This is where one would expect a field for the categories (say an ICollection) but the database is not built that way and I cannot modify the Entity Framework model.

When a category is selected in the view, the user has to select a property (a string list between 1 to 10. It will look like this:

 Category         Option
===============================
[x] Category 1   |---------|v|
[x] Category 2   |---------|v|
[x] Category 3   |---------|v|
[x] Category 4   |---------|v|

[x] - checkbox
|---|v| - dropdown list

However I’m thinking that perhaps I could create a ViewModel for this particular case. I was thinking something like

namespace Models.ViewModels
{
    public class ArticleViewModel
    {
        public Article Article { get; set; }
        public ICollection<Category> Categories { get; set; }

        public ArticleViewModel()
        {
            Categories = new List<Category>();
        }
    }
}

However when I post the form to the controller the ArticleViewModel and all of its internals are null. I’m not sure why.

The view itself is pretty basic, it will have two separate forms: one for modifying article details and one for assigning categories to the article.

As a guy new to asp.net mvc, I find it a little difficult to wrap my head around this problem.

Which approach would you recommend?

Update: added code for the view.

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Models.ViewModels.ArticleViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Modify article: <%= Model.Article.Title %>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% using (Html.BeginForm()) {%>
        <%= Html.ValidationSummary(true) %>

        <fieldset>
            <legend>Properties</legend>

            <div class="property">
                <span>
                    <%= Html.LabelFor(model => Model.Article.Title) %>

                    <%= Html.TextBoxFor(model => Model.Article.Title) %>
                    <%= Html.ValidationMessageFor(model => Model.Article.Title) %>
                </span>
            </div>

            <div class="property">
                <span>
                    <%= Html.LabelFor(model => Model.Article.Type) %>

                    <%= Html.TextBoxFor(model => Model.Article.Type) %>
                    <%= Html.ValidationMessageFor(model => Model.Article.Type) %>
                </span>
            </div>

            [continued...]
            <div>
                <%= Html.HiddenFor(model => Model.Article.Id) %>
                <input type="submit" value="Save" />
            </div>
        </fieldset>
    <% } %>
</asp:Content>

Routing looks like this:

routes.MapRoute(
    "Articles",
    "Articles/{action}/{id}",
    new { controller = "Articles", action = "Edit" }
);
  • 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-19T14:49:49+00:00Added an answer on May 19, 2026 at 2:49 pm

    The whole point of MVC is that your View is decoupled from your Model. It doesn’t matter what your EF entities look like.

    Create your view and view model classes the way you want. Don’t think about your DB structure. If you need a collection of Categories than create it. This code should represent the ‘domain’ of UI.

    In the controller map your view model to EF and vice versa. It’s controllers responsibility to understand data access code (or service layer code) and view code.

    This approach allows you to take a full advantage of MVC and decoupling between UI and database. Although you end up writing a bit more code for your models it pays up with interest later on (you can modify your db without touching UI and vice versa).

    If you find yourself spending a lot of time copying data from EF entities to models than you might want to take a look at this brilliant library: AutoMapper

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

Sidebar

Related Questions

I have a simple class that essentially just holds some values. I have overridden
I have a Hibernate class which is essentially just a wrapper around loads of
I would like to have a JPA Entity that has a GSon Object as
I'm trying to figure out how to do this. Essentially I have points A
If you have to use String.Replace() to replace test 50 times, you essentially have
I have essentially a survey that is shown, and people answer questions a lot
I am currently trying to manipulate a unordered list with jQuery, essentially I have
I have what is essentially a jagged array of name value pairs - i
i have a Sharepoint feature that essentially extends Lists with a new feature, using
Essentially, I have to get a flat file into a database. The flat files

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.