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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:52:00+00:00 2026-05-23T17:52:00+00:00

I can’t get a select list to bind to my ViewModel. I have a

  • 0

I can’t get a select list to bind to my ViewModel.

I have a ViewModel which contains a Question entity and a string

   public class QuestionViewModel
{
    public Question Question { get; set; }
    public string RefUrl { get; set; }

    public QuestionViewModel()
    {
    }

    public QuestionViewModel(Question question, string RefUrl)
    {
        this.Question = question;
        this.RefUrl = RefUrl;
    }

    public QuestionViewModel(Question question)
    {
        this.Question = question;
        this.RefUrl = "";
    }
}

this is the controller:

public ActionResult Edit(int id)
    {
        Question question = db.Question.Single(q => q.question_id == id);
        QuestionViewModel qvm = new QuestionViewModel(question);
        ViewBag.category_id = new SelectList(db.Category, "category_id", "category_name", qvm.Question.category_id);
        ViewBag.type_code = new SelectList(db.Question_Type, "type_code", "type_description", qvm.Question.type_code);
        return View(qvm);
    }

and the code in my view looks like this:

<div class="editor-label">
        @Html.LabelFor(model => model.Question.type_code, "Question_Type")
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => Model.Question.Question_Type, (SelectList)ViewBag.type_code)
        @Html.ValidationMessageFor(model => model.Question.type_code)
    </div>

The View does set the Question entity’s Question_Type to the selected value, but when i submit the form, the
ValidationMessageFor triggers??

  • 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-23T17:52:00+00:00Added an answer on May 23, 2026 at 5:52 pm

    What you have is not a view model. It’s a hybrid class that you have called view model and in which you have wrapped your domain entity (Question). That’s bad, don’t do it.

    Here’s what I would recommend you. Start by designing a real view model which will reflect the requirements of your view (from your current description it’s a dropdownlist containing some question types and allowing the user to select some question type from this ddl):

    public class QuestionViewModel
    {
        [DisplayName("Question_Type")]
        public string SelectedQuestionType { get; set; }
    
        public IEnumerable<SelectListItem> QuestionTypes { get; set; }
    
        // didn't see where you are using this on your view
        public string RefUrl { get; set; }
    }
    

    then have your controller map between your domain model and your view model. Of course a further improvement would be to use AutoMapper to avoid this mapping all over your controller actions:

    public ActionResult Edit(int id)
    {
        var question = db.Question.Single(q => q.question_id == id);
        var qvm = new QuestionViewModel
        {
            // preselect a value
            SelectedQuestionType = question.type_code,
            QuestionTypes = db.Question_Type.Select(x => new SelectListItem
            {
                Value = x.type_code,
                Text = x.type_description
            })
        };
        return View(qvm);
    }
    

    and then:

    <div class="editor-label">
        @Html.LabelFor(x => x.SelectedQuestionType)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(
            x => SelectedQuestionType, 
            new SelectList(Model.QuestionTypes, "Value", "Text")
        )
        @Html.ValidationMessageFor(x => x.SelectedQuestionType)
    </div>
    

    And one final remark: make sure you have gotten rid of any ViewBag/ViewData ugliness and put anything your view needs into the view model. You have shown some categories over there in your controller action which weren’t materialized in the view snippet you have shown. If you ever needed them simply put them in your view model, the same way we did with the question types.

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

Sidebar

Related Questions

Can you cast a List<int> to List<string> somehow? I know I could loop through
Can I get a 'when to use' for these and others? <% %> <%#
I have a jquery bug and I've been looking for hours now, I can't
Can I call select before recv_from on a socket that is blocking?
Can anyone suggest how to underline the title of a UIButton ? I have
Can someone maybe tell me why this is not working? I have used echo
Can I have a project that has some parts written in c and other
Can i get the source code for a WAMP stack installer somewhere? Any help
Can we put functions to determine the condition for our list comprehensions. Here is
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.