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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:29:01+00:00 2026-05-22T21:29:01+00:00

I have a property in my model very simple one: Now this dropDown doesn’t

  • 0

I have a property in my model very simple one:

enter image description here

Now this dropDown doesn’t work right

@Html.DropDownListFor(m => m.Camp, new SelectList(ViewBag.Camps, "Id", "Name"))

it returns null instead of a chosen Camp, but if I change that into:

@Html.DropDownListFor(m => m.Camp.Id, new SelectList(ViewBag.Camps, "Id", "Name"))

It would return me a Camp object with correct Id, but the Name would be still null.

Why?

UPD:

And now another problem is if I choose the second approach it would screw up with unobtrusive validation. Although I’ll be able to get the right camp based on the chosen id.

  • 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-22T21:29:01+00:00Added an answer on May 22, 2026 at 9:29 pm

    That’s normal. Only the Id is posted to the controller action. That’s how dropdown inside forms work. So that’s all you can hope to get there. You will then use this Id to get the corresponding Camp object from the database:

    [HttpPost]
    public ActionResult Foo([Bind(Prefix = "Camp")]int id)
    {
        Camp camp = Repository.GetCamp(id);
        ...
    }
    

    Also please get rid of this ViewBag and use a real view model:

    public class CampViewModel
    {
        public int Id { get; set; }
        public IEnumerable<SelectListItem> Camps { get; set; }
    }
    

    and in the controller:

    public ActionResult Index()
    {
        var model = new CampViewModel
        {
            Camps = Repository.GetCamps().Select(x => new SelectListItem
            {
                Value = x.Id.ToString(),
                Text = x.Name
            })
        };
        return View(model);
    }
    
    [HttpPost]
    public ActionResult Index(int id)
    {
        Camp camp = Repository.GetCamp(id);
        ...
    }
    

    and the view:

    @model CampViewModel
    @using (Html.BeginForm())
    {
        @Html.DropDownListFor(
            x => x.Id,
            new SelectList(Model.Camps, "Value", "Text")
        )
        <input type="submit" value="OK" />
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Tapestry PropertyModel for gender. Right now the dropdown just shows Male
Consider a very simple model where we have locations and each location can have
I have very simple situation and really don't have a clue why this isn't
I have a very simple user model. When I first created it, it had
I have a very simple object model with a base class Person and two
I have a simple question. I have a model that looks like this: public
I have a very simple Core data Model comprising of a single Gallery which
very simple problem here... but I can't work it out. I have a simple
How do I bind a view model property to the ListBox.SelectedItem property? I have
I have the following classes in my ActiveRecord model: def Property < ActiveRecord::Base #

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.