.NET MVC: Removing items from List in model

Viewed 13

So here's my problem. I've been trying to solve this for weeks and nothing, so I'm biting the bullet and asking for help.

Basically, I'm trying to write a computer encyclopedia-cum-inventory management-cum-auditing program. Seeing that MVC is all the rage these days I decided to step out of my comfort zone of classic .NET and try MVC.

I have a model with partially the following fields:

public class SoundCard
{
    public Guid Id { get; set; }
    ...
    public virtual List<SoundChipset>? SoundChipsets { get; set; }
    ...
    public virtual List<MidiSynthChipset>? MidiSynthChipsets { get; set; }
    ...
}

The model is scaffolded into creating a controller and then a set of view pages. The add view works brilliantly and I could add sound and midi chipsets as needed. The edit view is where my problem lies: I could add new sound chipsets and midi chipsets, but could not remove the added ones.

The partial code for the controller for edit is as follows:

    public async Task<IActionResult> Edit(Guid id, [Bind("Id,ModelSeries,ModelName,ModelNumber,ReleaseDate,HasCDROMInterface,HasSCSIPort,HasIDEPort,HasNECCDPort,HasMatsushitaCDPort,HasWaveBlasterPort,HasRAMForWaveTable,RAMSizeKB,HasGamePort,HasMPU401Port,numAudioOutPorts,numAudioInPorts,numAudioBiDirectionalPorts,numCoaxInPorts,numCoaxOutPorts,numOpticalInPorts,numOpticalOutPorts,numMidiInPorts,numMidiOutPorts")] SoundCard soundCard)
    {
    ...
                string[] selectedSndChipsets = Request.Form["lbSoundChipsetsSelected"].ToArray();
                List<SoundChipset> sndChipset = new List<SoundChipset>();

                foreach (string uuid in selectedSndChipsets)
                {
                    sndChipset.Add(Factories.SoundChipsetDDLFactory.getSoundChipsetByUUID(_context, uuid));
                }

                soundCard.SoundChipsets = sndChipset;

                string[] selectedMidChipsets = Request.Form["lbMidiChipsetsSelected"].ToArray();
                List<MidiSynthChipset> MidChipsets = new List<MidiSynthChipset>();

                foreach (string uuid in selectedMidChipsets)
                {
                    MidChipsets.Add(Factories.MidiSynthChipsetDDLFactory.getMidiSynthChipsetByUUID(_context, uuid));
                }
                soundCard.MidiSynthChipsets = MidChipsets;

                _context.Update(soundCard);
                await _context.SaveChangesAsync();
     ...

So, practically recreating the Sound Chipsets and Midi Chipsets lists from scratch every single time. Problem is, the program treats the list as new objects to add to the existing list, it does not erase the current list despite the list being a new one!

I've tried to apply a Clear() command to the list but instead the program tossed an NullReferenceException which is puzzling because the list is supposed to be populated.

For completeness sake, here's part of the code for the edit frontend. It's partially JS to handle moving items between two boxes:

               <label asp-for="SoundChipsets" class="control-label"></label>
            <table>
                <tr>
                    <th>Available</th>
                    <th>&harr;</th>
                    <th>Selected</th>
                </tr>
                <tr>
                    <td>
                        @Html.ListBox("lbAllSoundChipsets",(IEnumerable<SelectListItem>)ViewBag.SoundChipsets, new {@id="lbAllSoundChipsets", @style="min-width: 250px;"})
                    </td>
                    <td>
                        <input onclick="Javascript:SwitchListBoxItems('lbAllSoundChipsets', 'lbSoundChipsetsSelected');" type="button" value="&rarr;" /><br />
                        <input onclick="Javascript:SwitchListBoxItems('lbSoundChipsetsSelected', 'lbAllSoundChipsets');" type="button" value="&larr;" />
                    </td>
                    <td>
                        @Html.ListBox("lbSoundChipsetsSelected",(IEnumerable<SelectListItem>)ViewBag.SelectedSoundChipsets, new {@id="lbSoundChipsetsSelected", @style="min-width: 250px;"})
                    </td>
                </tr>
            </table>
        </div>
        <div class="form-group">
            <label asp-for="MidiSynthChipsets" class="control-label"></label>
            <table>
                <tr>
                    <th>Available</th>
                    <th>&harr;</th>
                    <th>Selected</th>
                </tr>
                <tr>
                    <td>
                        @Html.ListBox("lbAllMidiChipsets",(IEnumerable<SelectListItem>)ViewBag.MidiSynthChipsets, new {@id="lbAllMidiChipsets", @style="min-width: 250px;"})
                    </td>
                    <td>
                        <input onclick="Javascript:SwitchListBoxItems('lbAllMidiChipsets', 'lbMidiChipsetsSelected');" type="button" value="&rarr;" /><br />
                        <input onclick="Javascript:SwitchListBoxItems('lbMidiChipsetsSelected', 'lbAllMidiChipsets');" type="button" value="&larr;" />
                    </td>
                    <td>
                        @Html.ListBox("lbMidiChipsetsSelected",(IEnumerable<SelectListItem>)ViewBag.SelectedMidiSynthChipsets, new {@id="lbMidiChipsetsSelected", @style="min-width: 250px;"})
                    </td>
                </tr>
            </table>

And the JS code:

        function SwitchListBoxItems(sourceListItem, targetListItem) {
        var src = document.getElementById(sourceListItem);
        var dest = document.getElementById(targetListItem);
        if (dest != null && src != null) {
            while (src.options.selectedIndex >= 0) {
                var lstItemNew = new Option(); // Create a new instance of ListItem
                lstItemNew.text = src.options[src.options.selectedIndex].text;
                lstItemNew.value = src.options[src.options.selectedIndex].value;
                dest.options[dest.length] = lstItemNew;
                src.remove(src.options.selectedIndex);
            }
        }

So yeah, if someone can point me in the right direction to get the system to delete items.

Thanks in advance.

0 Answers
Related