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()});
}
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.Suggested refactoring:
It’s silly to pass in the ViewModel when all your using it for is taking the necessary Guid. Then call it like
If you’re query still isnt adding the correct item try debugging it like.
If you’re not finding it, and you’re certain lastId is set correctly, it’s probably a database issue.