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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:20:09+00:00 2026-05-26T23:20:09+00:00

I have a Search Edit view which is strongly typed to my Search model

  • 0

I have a Search Edit view which is strongly typed to my Search model class seen below (simplified).

Searches and Custodians

I want to display the custodians that are attributed to the Search being edited in a listbox showing all Custodians, with the current ones selected.

My controller’s Get Edit action is thus:

public ActionResult Edit(int id, int searchListId = 0)
{
    if (searchListId != 0)
    {
        Session["CurrentSearchListID"] = searchListId;  
    }
    ProjectContext mydb = db;
    Search search = Search.Find(mydb, id);
    
    IEnumerable<SelectListItem> selectedItems =
        from c in Custodian.List(mydb)
        select new SelectListItem
        {
            Selected = (search.Custodians.Contains(c)),
            Text = c.CustodianName,
            Value = c.ToString()
        };

    ViewBag.Custodians = selectedItems;
    return View(search);
}

And my Views listbox is thus:

        @{
            //List<Kiersted.Keps.BusinessObjects.Custodian> Custodians = ViewBag.Custodians;
            IEnumerable<SelectListItem> SelectedItems = ViewBag.Custodians;
        }

        @Html.ListBox("Custodians", SelectedItems);

This produces a listbox with the Custodians depicted, but none are selected (I have confirmed that several of the SelectListItems accurately describe the custodian as selected. I have tried using ListBoxFor and it produces the same thing when populated with a MultiSelectList.

Finally I decided to just force it to do what I want, but this does not return selected Custodians on Submit.

<select id="Custodians" multiple="multiple" name="Custodians">
@foreach (Kiersted.Keps.BusinessObjects.Custodian cust in Custodians)
{
    if (Model.Custodians.Contains(cust))
    {
    <option value="@cust.CustodianID" selected="selected">@cust.CustodianName</option>
    }
    else
    {
    <option value="@cust.CustodianID" >@cust.CustodianName</option>
    }
}
</select>

Anyone know how you are supposed to do this?

Edits:

ListBoxFor example

OK so after fiddling around with it for a while longer, I have now gotten Custodians selected in the listbox that correspond to the Search Custodians. Below is the view code:

<div class="editor-field">
    @Html.ListBoxFor(model => model.Custodians, allCustodians.Select(cust => new SelectListItem {
                           Text = cust.CustodianName,
                           Value = cust.CustodianID.ToString(),
                           Selected = true}), 
                           new { Multiple = "multiple" })
</div>

If I select several more custodians, how do I get them (or their corresponding values rather) back to the control upon submit?

  • 1 1 Answer
  • 3 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-26T23:20:10+00:00Added an answer on May 26, 2026 at 11:20 pm

    Seeing that after your edit the problem boils down to multiple select model binding, perhaps you will find these useful?

    How does a multiple select list work with model binding in ASP.NET MVC?

    http://ittecture.wordpress.com/2009/04/30/tip-of-the-day-198-asp-net-mvc-listbox-controls/

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

Sidebar

Related Questions

I have an edit text which functions as a search box in my application.
I have 3 pages: search.html. search.php, and edit.php I also have new.php which I
I want to implement search bar in my application which will filter list view
Well i have multiple model and i want to allow the customer to search
I want to have a php file that can be used to edit, view,
I have successfully increased the zIndex for edit,add,del and search options but alertmod is
I have search screen which has 10 fields and involves around 5 to 6
the div on website below that have search at the bottom and it always
I have a search page which finds candidates. From this page you can click
Is this possible? I have a function which creates a table. I want a

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.