Cybercop
Cybercop

Reputation: 8674

During edit, not showing current value as list of available item in dropdownlist

Current situation during edit

I have a edit form where user will be allowed to change the ownership(provide it to new customer) of a hardware (module). What I've done is, through a function collected all the available customers. This list is shown in dropdown, which also contains current owner(customer) of the hardware.

What I want to achieve is in the dropdownlist, I just want to remove the current owner (also one of the customer).

Here is the code view

ViewModel

public class ChangeOwnership
{
    public ChangeOwnership()
    {
    }

    public Guid CurrentOwnerId { get; set; }

    public string CurrentOwner { get; set; }

    public string SelectedNewOwner { get; set; }

    public IEnumerable<SelectListItem> OwnerNames { get; set; }  
}

Get method in controller

[HttpGet]
    public ActionResult ChangeModuleOwnership(long id)
    {
        var owners = _ownedModuleRepository.GetAllBusinessUnits();//businessunits = customers
        var model = new ChangeOwnership
            {
                CurrentOwnerId = _ownedModuleRepository.GetOwnedModuleOwnerId(id),
                CurrentOwner = _ownedModuleRepository.GetSelectedModuleOnwerName(id),
                OwnerNames = owners.Select(m=> new SelectListItem
                    {
                        Value = m.Id.ToString(),
                        Text = m.Name
                    })
            };

        return View(model);
    }

Repository function

List<BusinessUnit> IOwnedModuleRepository.GetAllBusinessUnits()
    {
        return _dbSis.BusinessUnits.ToList();
    }

What do i need to do so that I can filter out the current owner from the dropdownlist so that I can only show potential new owners(customers)?

Upvotes: 0

Views: 333

Answers (1)

PostureOfLearning
PostureOfLearning

Reputation: 3541

why don't you do something like this in your controller:

OwnerNames = owners
    .Where(o => o.OwnerID != id) //Filter out the current owner here
    .Select(m=> new SelectListItem
                    {
                        Value = m.Id.ToString(),
                        Text = m.Name
                    })

Upvotes: 1

Related Questions