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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:05:13+00:00 2026-05-15T07:05:13+00:00

I have following problem. In my view model I defined some list properties as

  • 0

I have following problem. In my view model I defined some list properties as follows:

public class BasketAndOrderSearchCriteriaViewModel
{
    List<KeyValuePair> currencies;
    public ICollection<KeyValuePair> Currencies
    {
        get
        {
            if (this.currencies == null)
                this.currencies = new List<KeyValuePair>();
            return this.currencies;
        }
    }

    List<KeyValuePair> deliverMethods;
    public ICollection<KeyValuePair> DeliveryMethods
    {
        get
        {
            if (this.deliverMethods == null)
                this.deliverMethods = new List<KeyValuePair>();
            return this.deliverMethods;
        }
    }
 }

This view model is embedded in another view model:

 public class BasketAndOrderSearchViewModel
 {
    public BasketAndOrderSearchCriteriaViewModel Criteria
    {
        [System.Diagnostics.DebuggerStepThrough]
        get { return this.criteria; }
    }
 }

I use 2 action methods; one is for the GET and the other for POST:

[HttpGet]
public ActionResult Search(BasketAndOrderSearchViewModel model){...}

[HttpPost]
public ActionResult SubmitSearch(BasketAndOrderSearchViewModel model){...}

In the view I implement the whole view model by using the EditorFor-Html Helper which does not want to automatically display DropDownLists for List properties!
1. Question: How can you let EditorFor display DropDownLists?

Since I could not figure out how to display DropDownLists by using EditorFor, I used the DropDownList Html helper and filled it through the view model as follows:

    public IEnumerable<SelectListItem> DeliveryMethodAsSelectListItem()
    {
        List<SelectListItem> list = new List<SelectListItem>();
        list.Add(new SelectListItem()
        {
            Selected = true,
            Text = "<Choose Delivery method>",
            Value = "0"
        });
        foreach (var item in this.DeliveryMethods)
        {
            list.Add(new SelectListItem()
            {
                Selected = false,
                Text = item.Value,
                Value = item.Key
            });
        }

        return list;
    }

My 2. question: As you can see I pass my view model to the action metho with POST attribute! Is there a way to get the selected value of a DropDownList get binded to the passed view model? At the moment all the DropDownList are empty and the selected value can only be fetched by the Request.Form which I definitely want to avoid!

I would greatly appreciate some ideas or tips on this!

  • 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-15T07:05:13+00:00Added an answer on May 15, 2026 at 7:05 am

    Let’s try to take on this one:

    Answer to Question 1: How can you let EditorFor display DropDownLists?

    When you call Html.EditorFor() you can pass extra ViewData values to the EdiorTemplate View:

    <%: Html.EditorFor(model => Model.Criteria, new { DeliveryMethods = Model.DeliveryMethods, Currencies = Model.Currencies}) %>
    

    Now you have ViewData["DeliveryMethods"] and ViewData["Currencies"] initialized and available inside your EditorTemplate.

    In your EditorTemplate you somehow need to call and convert those entries into DropDowns / SelectLists.
    Assuming you’ve got an ascx file of type System.Web.Mvc.ViewUserControl<BasketAndOrderSearchCriteriaViewModel> you could do the following:

    <%: Html.LabelFor(model => model.DeliveryMethods) %>
    <%: Html.DropDownList("SelectedDeliveryMethod", new SelectList(ViewData["DeliveryMethods"] as IEnumerable, "SelectedDeliveryMethod", "Key", "value", Model.SelectedDeliveryMethod)) %>
    

    Same goes for the Currencies.

    <%: Html.LabelFor(model => model.Currencies) %>
    <%: Html.DropDownList("SelectedCurrency", new SelectList(ViewData["Currencies"] as IEnumerable, "SelectedDeliveryMethod", "Key", "value", Model.SelectedCurrency)) %>
    

    This setup will make your DeliveryMethodAsSelectListItem() obsolete and you can use any kind of list. Means you are not bound to KeyValuePairs. You’ll just need to adjust your call on Html.DropDownList() from now on.

    As you can see, I have introduced some new properties to your BasketAndOrderSearchCriteriaViewModel:

    Model.SelectedDeliveryMethod
    Model.SelectedCurrency
    

    They are used to store the currently selected value.

    Answer to Question 2: Is there a way to get the selected value of a DropDownList get binded to the passed view model?

    In the EditorFor template we are passing the newly created Model.SelectedDeliveryMethod and Model.SelectedCurrency properties as the SelectedValue Parameter (See 4th Overload of the DropDownList Extension Method).

    Now that we have the View doing it’s job: How can we get the currently selected value inside the POST Action?

    This is really easy now:

    [HttpPost]
    public ActionResult SubmitSearch(BasketAndOrderSearchViewModel model)
    {
        ...
        var selectedDeliveryMethod = model.Criteria.SelectedDeliveryMethod;
        var selectedCurrency model.Criteria.SelectedDeliveryMethod;
        ...
    }
    

    Note: I don’t have an IDE to test it right now, but it should do the trick or at least show you in which direction to go.

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

Sidebar

Related Questions

I have the following problem using template instantiation [*]. file foo.h class Foo {
I have the following Form defined class MyForm(ModelForm): def __init__(self, readOnly=False, *args, **kwargs): super(MyForm,self).__init__(*args,**kwrds)
I have the following problem: I have an HTML textbox ( <input type=text> )
I have the following problem using subversion: I'm currently working on the trunk of
I have the following problem in my Data Structures and Problem Solving using Java
I have the following problem: I open the dialog, open the SIP keyboard to
I have the following problem. If I query values with a keyfigure which is
Avast there fellow programmers! I have the following problem: I have two rectangles overlapping
I have got the following problem since the server has safe mode turned on,
Has anyone encountered the following problem: I have IIS7 running on my computer. On

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.