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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:09:24+00:00 2026-06-02T19:09:24+00:00

I’m developing a small app in order to better understand how MVC3 anda Razor

  • 0

I’m developing a small app in order to better understand how MVC3 anda Razor works. I’m using MVC3, all code was generated automatically (dbContext via T4, Controller via Add Controller, Databese from EDMX model…).

In my model I have this simple model:
https://i.stack.imgur.com/nyqu4.png

public partial class Application
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ApplicationType ApplicationType { get; set; }
}

public partial class ApplicationType
{
    public int Id { get; set; }
    public string Type { get; set; }
}

As you can see, ApplicationType is basically an enum (shame that EF 4 has no support for enums). So, in my ApplicationController I have this:

public ActionResult Create()
{
    ViewBag.AppTypes = new SelectList(db.ApplicationTypes.OrderBy(c => c.Type), "Id", "Type");
    return View();
} 

[HttpPost]
public ActionResult Create(Application application)
{
    if (ModelState.IsValid)
    {
        db.Applications.Add(application);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(application);
}

And in my view:

@Html.DropDownListFor(model => model.ApplicationType.Id, (SelectList)ViewBag.AppTypes, "Choose...")

Now I’m facing two problems:

1) ApplicationType not being populated:

As @Html.DropDownListFor renders only a simple select, it fills the ID, but does not fill Type property as you can see below (sorry, I can’t post images as I’m new here):

https://i.stack.imgur.com/96IR1.png

In the picture you can see that the ID is ok, but Type is empty.

What I’m doing wrong?

2) Duplicated Data

The second problem is that if I fill the Type property manually during debug (simulating a correct workflow scenario), ApplicationType is being duplicated in the database, instead of only referring to an old registry.

So, how can I make @Html.DropDownListFor refer to a previous existing item instead of creating a new one?

Thanks for your help!

  • 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-02T19:09:25+00:00Added an answer on June 2, 2026 at 7:09 pm

    I believe the mistake you’re making is using your domain models in the view and assuming that on post the entire model should be completely binded and ready to store in the database. While it is possible to use domain models in the view, it’s better practice to create separate View Models.

    For example :

    public class ApplicationViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
    
        public SelectList ApplicationTypeList { get; set; }
        public string ApplicationTypeId { get; set; }
    }
    

    In your view:

    @Html.DropDownListFor(model => model.ApplicationTypeId, Model.ApplicationTypeList , "Choose...")
    

    In your controller

    [HttpPost]
    public ActionResult Create(ApplicationViewModel model)
    {
        if (ModelState.IsValid)
        {
            Application application = new Application()
            {
               Id = model.Id,
               Name = model.Name,
               ApplicationType = db.ApplicationTypes
                  .First(a => a.Id == model.ApplicationTypeId);
    
            };
            db.Applications.Add(application);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    
        return View(model);
     }
    

    You can then make verifying that your View Model’s ApplicationTypeId corresponds to a real application type part of your modelstate’s verification. You can use AutoMapper to speed up the process of converting view models to domain models.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.