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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:41:36+00:00 2026-05-21T03:41:36+00:00

I am presently working on a application in which I have a display a

  • 0

I am presently working on a application in which I have a display a list of items in a list box in the view and then send back the selected items to the controller.

My model is as follows:

public class Items
    {
        [DisplayName("Items")]
        public string[] Items { get; set; }
    }

When the user first requests the page, the list of items has to be queried from a database and sent to the view.
I am able to figure out how to collect the items into ArrayList/string[] at the controller side but am not able to understand the syntax for binding the view with the model and displaying the list using Html.ListboxFor and sending back the model on form submit.

Can someone please help me.

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-21T03:41:36+00:00Added an answer on May 21, 2026 at 3:41 am

    View model:

    public class MyViewModel
    {
        [DisplayName("Items")]
        public string[] SelectedItemIds { get; set; }
        public IEnumerable<SelectListItem> Items { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                // preselect some items
                // leave empty if you want none to be selected initially
                SelectedItemIds = new[] { "1", "3" },
    
                // Normally you would fetch those from your database
                // hardcoded here for the purpose of the post             
                Items = Enumerable.Range(1, 10).Select(x => new SelectListItem
                {
                    Value = x.ToString(),
                    Text = " item " + x
                })
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(string[] selectedItemIds)
        {
            // here you will get the list of selected item ids
            // where you could process them
            // If you need to redisplay the same view make sure that 
            // you refetch the model items once again from the database
            ...
    
        }
    }
    

    View (Razor):

    @model AppName.Models.MyViewModel
    @using (Html.BeginForm())
    {
        @Html.LabelFor(x => x.SelectedItemIds)
    
        @Html.ListBoxFor(
            x => x.SelectedItemIds, 
            new SelectList(Model.Items, "Value", "Text")
        )
        <input type="submit" value="OK" />
    }
    

    View (WebForms):

    <% using (Html.BeginForm()) { %>
        <%= Html.LabelFor(x => x.SelectedItemIds) %>
    
        <%= Html.ListBoxFor(
            x => x.SelectedItemIds, 
            new SelectList(Model.Items, "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 working JNLP application which I need to distribute to various non-technical
I have an application, built using MVC, that produces a view which delivers summary
I am working on an application in which, i have to read XML files
My application is in Asp.net MVC3 coded in C#.Net. I have a view which
Currently I am working on an application which depends on a lot of external
I'm working on a web application which will be used for classifying photos of
I have an MVC 2.0 application that I'm playing around with in VS2010 which
In my application i have 6 different view controllers. 1. Home page 2. A
I have working application in asp.net MVC3. Today when I was working on it
I have a MonoTouch application that generates some PDFs locally and then prints them

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.