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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:46:41+00:00 2026-06-02T18:46:41+00:00

Currently I am trying to check a selected Item Id and display in dropdown

  • 0

Currently I am trying to check a selected Item Id and display in dropdown list on an Edit page which will show the previous selected item. The Items show based on their active status. All active items are displayed in the dropdown and if the previous selection choice for the user is currently inactive, it should still default to that item in the dropdown list since it was the selection previously made. My issue is that i am not able to show the inactive item that was selected by the user on the edit page. I will post all my code and this is a continuation of a question asked in a different link: passing null parameters

my Code in CONTROLLER:

private IEnumerable<SearchItems> GetItems(ItemDescriptionFormViewModel viewModel = null)
{
    if(viewModel == null)
      viewModel = new AppointmentViewModel();
    IOrderedQueryable<ItemDescription> items= _itemDescriptionRepository.FindAll().OrderBy(
        c => c.Sort == null).ThenBy(
            c => c.Sort).ThenBy(c => c.Description);

    if(items.Count()==0)
        ModelState.AddModelError("", string.Format("No active {0} entered.", Kids.Resources.Entities.ItemDescription.EntityNamePlural));
   return
        _itemDescriptionRepository.FindAll().OrderBy(c => c.Description).Where(a=>a.IsActive == true || a.ItemDescriptionId == viewModel.ItemDescriptionId).Select(
            c => new SearchItems {Text = c.Description, Value = c.ItemDescriptionId.ToString()});
}

My Edit Method in CONTROLLER:

[HttpGet]
    [AppointmentAuthorization]
    public ActionResult Edit(Guid appointmentId)
    {
        Appointment appointment = _appointmentService.Get(appointmentId);
        if (appointment == null) return View("NotFound");


        var viewModel = new AppointmentViewModel
                            {
                                AppointmentId = appointment.AppointmentId,

                            };
        viewModel.Items= GetItems();

        return this.RazorView("Edit", viewModel);
    }

It seems like something is missing and passing viewModel as a parameter in viewModel.Items= GetItems() didn’t do much. I also have another Post method for Edit and a Create method all which call the GetItems() method. Any help will be great. Thanks 😀

This is what i have in my ViewModel class:

public IEnumberable<SearchItems> Items {get; set;}
public Guid ItemDescriptionid {get; set;}

Create Method:

[HttpGet]
    [AppointmentAuthorization]
    public ActionResult Create(Guid caseId)
    {
        var viewModel = new AppointmentViewModel
                            {
                               Items= GetItems()
                            };

        return this.RazorView("Create", viewModel);
    }

*****SOLUTION*****

private IEnumerable<SearchItems> GetItems(Appointment appointment)
    {
        IEnumerable<short?> itemDescriptionIds =
            appointment.AppointmentItems.Where(c => c.ItemDescriptionId.HasValue).Select(
                c => c.ItemDescriptionId).Distinct();

        IOrderedQueryable<ItemDescription> itemDescription =
            _itemDescriptionRepository.FindAll().Where(
                a => a.IsActive == true || itemDescriptionIds.Contains(a.ItemDescriptionId)).OrderBy(
                    d => d.Description);

        return itemDescription.Select(c=> new SearchItems{Text = c.Description, Value = c.ItemDescriptionId.ToString()});
    }
  • 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-06-02T18:46:42+00:00Added an answer on June 2, 2026 at 6:46 pm

    The reason your inital code wasn’t querying the previously selected value was because your query a.ItemDescriptionId == viewModel.ItemDescriptionId, compares item description id, but in your View Model you only set appointment id. If you need item description id then make sure you initialize it.

    var viewModel = new AppointmentViewModel
    {
         AppointmentId = appointment.AppointmentId,
         ItemDescriptionId = appointment.ItemDescriptionId
    };
    

    Suggested refactoring:

    private IEnumerable<SearchItems> GetItems(Guid lastId = new Guid())
    {
        IOrderedQueryable<ItemDescription> items= _itemDescriptionRepository.FindAll();
    
        if(!items.Any())
            ModelState.AddModelError("", string.Format("No active {0} entered.", Kids.Resources.Entities.ItemDescription.EntityNamePlural));
    
        return items
            .OrderBy(c => c.Description).Where(a=>a.IsActive == true || a.ItemDescriptionId == lastId)
            .Select(c => new SearchItems {Text = c.Description, Value = c.ItemDescriptionId.ToString()});
    }
    

    It’s silly to pass in the ViewModel when all your using it for is taking the necessary Guid. Then call it like

    viewModel.Items= GetItems(appointment.ItemDescriptionId);
    

    If you’re query still isnt adding the correct item try debugging it like.

    var testExists = itemDescriptionRepository.FindAll().FirstOrDefault(a.ItemDescriptionId == lastId);
    

    If you’re not finding it, and you’re certain lastId is set correctly, it’s probably a database issue.

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

Sidebar

Related Questions

I am currently trying to write some code that will accept some FTP details,
I'm currently trying to create a C source code which properly handles I/O whatever
I'm trying to check if a certain category is allready selected by looping through
Currently I am trying to check the screen boundaries by seeing if a CCSprite
I am currently trying to sort out some logic to check an array for
I am currently trying to use javascript to display an extra form field if
What I am trying to achieve is to check all dropdowns on the page
Check out my example here: http://timkjaerlange.com/foobar/test.html I'm currently trying to apply a class to
I am currently trying to create a multi-page form that uses both jQuery and
I'm currently faced with a dilemma. I'm trying to build a menu that will

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.