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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:38:29+00:00 2026-05-19T23:38:29+00:00

This question has been asked in various forms but none of the answers seem

  • 0

This question has been asked in various forms but none of the answers seem to fit my situation. I am simply trying to retrieve the selected value of a dropdown list in my controller.

Here is my code:

ViewModel.cs

public class ViewModel
{
  public ViewModel() {}
  public ViewModel(Contact contact, IEnumerable<State> states)
    {
      this.Contact = contact;
      this.States = new SelectList(states, "Id", "Name", contact.StateId);
    }
  public Contact Contact {get;set;}
  public SelectList States {get;set;}
}

Controller.cs

[HttpPost]
public ActionResult Edit(ViewModel viewModel)
{
  _contactService.UpdateContact(viewModel.Contact);
  return RedirectToAction("Item", new {id = viewModel.Contact.Id});
}

View.cshtml

<button type="submit" onclick="javascript:document.update.submit()"><span>Update</span></button>//aesthic usage. 
@{using (Html.BeginForm("Edit", "Controller", FormMethod.Post, new { name = "update" }))
  {
   @Html.HiddenFor(m => m.Contact.Id)
   @Html.LabelFor(m => m.Contact.Name, "Name:")
   @Html.TextBoxFor(m => m.Contact.Name)

   <label for="state">State:</label>
   @Html.DropDownList("state", Model.States)
}
}

Everything works as expected except that no values from the dropdownlist are passed in my posted viewModel to the controller. The edit page and all fields load correctly. The dropdowns bind correctly and have their selected values displayed properly. However, when I post I only get a “Contact” object passed to the controller. The “States” SelectList object is null.

I tried mapping a “StateId” property in my viewModel contstructor but that did not work either. What am I doing wrong?

Thanks.

  • 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-19T23:38:30+00:00Added an answer on May 19, 2026 at 11:38 pm

    I hate answering my own questions but based on the multiple issues I had coupled with the myriad of answers available I thought I would summarize my findings.

    First off thanks for Filip, his answer did not exactly fix my problem but it led me in the right direction. +1

    If you are creating a form for viewing and editing that requires a drop down list, here are some suggestions and gotchas. I will start with a list of parameters that I needed to fit my needs.

    1. Strongly typed views in my view are preferable. Minimize magic strings.
    2. View models should contain as little logic and extraneous elements as possible. There only job should be to facilitate a collection of data objects.
    3. The drop down list should display the selected value.
    4. The selected value should map easily back to the view model on form submit.

    This may sound like an obvious and easily obtainable list but for someone new to MVC, it is not. I will revise my code from above with comments. Here is what I did.

    ViewModel.cs

    public class ViewModel
    {
      public ViewModel() {}
      public ViewModel(Contact contact, IList<State> states)
      {
    //no need to pass in a SelectList or IEnumerable, just what your service or repository spits out
        this.Contact = contact;
        this.States = states;
      }
      public Contact Contact {get;set;}
      public IList<State> States {get;set;}
    }
    

    Controller.cs //nothing really different than above

    public ActionResult Edit(int id)
    {
      var contact = _contactService.GetContactById(id);
      var states = _stateService.GetAllStates();
      return View(new ViewModel(contact, states));
    }
    
    public ActionResult Edit(ViewModel viewModel)
    {
      _contactService.UpdateContact(viewModel.Contact);
      return RedirectToAction("Edit", new {id = viewModel.Contact.Id });
    }
    

    View//thanks goes to Artirto at this post

    @{using (Html.BeginForm("Edit", "Controller", FormMethod.Post))
     {
      @Html.HiddenFor(m => m.Contact.Id)
      @Html.DropDownListFor(m => m.Contact.StateId, new SelectList(Model.States, "Id", "Name", @Model.Contact.StateId))
    <input type="submit" value="Save" />
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been asked in various forms on here before, but I cannot
This question has no doubt been asked in various forms in the past, but
This question has been asked in various forms in a number of different forums,
This question has been asked before but the answers aren't always clear or are
This question has been asked before somewhat, but I hope mine differs. My situation
This question has been asked on here a few times, but none of the
This question has been asked before but in relation to performance. The SO answers
This question has been asked a million times but I can't seem to find
This question has been asked in various permutations, but I haven't found the right
This question has been asked many times before, but they all seem to relate

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.