mardi 19 mai 2020

Net Core 3.0: multiple checkboxes with integer variables

In my web app I have a self referencing many to many relationship of plants. The M2M relationships are good neighbours and bad neighbours. I want to be able to have a form where a user can check off the both types of neighbours and then save the form.

What I have so far: For brevity, I will only show code to Good neighbours relation, the bad neighbours is the same.

Models

public class Plant
{
   public int Id { get; set; }
   public string Name { get; set; } 
   public virtual ICollection<GoodPlants> GoodNeighbours { get; set; }
}

public class GoodPlants
{
   public int PlantId { get; set; }
   public int GoodNeighbourId { get; set; }

   public virtual Plant Plant { get; set; }
   public virtual Plant GoodNeighbour {get; set;}
}

I have an Edit method to display the edited plant and all the other plants:

var plant = await _context.Plants.FindAsync(id);
ICollection<Plant> plants = _context.Plants               
   .ToList();
ICollection<GoodPlants> goodNeighbours = await _context.GoodNeighbours
   .Where(g => g.PlantId == id)
   .Include(g => g.GoodNeighbour)
   .ToListAsync();

GoodPlants ownGoodPlant = goodNeighbours.FirstOrDefault(i => i.GoodNeighbour.Id == plant.Id);
Plant ownPlant = plants.FirstOrDefault(i => i.Id == plant.Id);
goodNeighbours.Remove(ownGoodPlant);
plants.Remove(ownPlant);

This desperately needs refactoring, but thats not the main issue here.

In my view I contrast the elements of AllPlants collection if it appears on either of the neighbours collections and have the checkbox set to checked or not:

@using (Html.BeginForm("EditPlant", "Plants", FormMethod.Post))
{
<table class="table">
   @foreach(var item in Model.AllPlants)
   {
   <tr>
   @if (Model.Goodneighbours.Any(g => g.GoodNeighbour.Id == item.Id))
   {
       <td>
          @Html.Label(item.Name)
       </td>
       <td>
          <input type="checkbox"
               name="GoodNeighbours"
               value="@item.Id"
               @(Html.Raw("checked=\"checked\"")) />
        </td>
    }
    else
    {
       <td>
          @Html.Label(item.Name)
       </td>
       <td>
          <input type="checkbox"
               name="GoodNeighbours"
               value="@item.Id" />
       </td>
     }
     </tr>
   }
</table>
}

I want to know how I can keep track of all the selected items (and unselected), get them in my viewmodel and send them back to my Edit method that handles the POST request and store them in my database.

Thanks in advance, I'm pretty stuck!




Aucun commentaire:

Enregistrer un commentaire