Tuesday, March 19, 2019

Adding Objects to a Model (empty List) inside View using a "Many-to-Many" Relationship

Hey guys, so I'm fairly new with .Net and I've been trying to figure how to do this correctly but I've been struggling quite a bit. So in case you can take a minute to help me, here's the deal. If you see something very stupid in my code, I'd like to remind you that I'm not totally confortable with this, so my apologies. Also, if this is not the right place to leave a question like this, or if you have any helpful link that can lead me to a possible solution, please leave it here. (Thank you very much!)

Given the following Models:

public class Project

{

[Key, Required]

public int ProjectID { get; set; }

[Required]

public string Title { get; set; }

public string Description { get; set; }

public System.DateTime Date_Proposal { get; set; }

public int Up_Votes { get; set; }

public int Down_Votes { get; set; }

public virtual List<ProjectSector> ProjectSectors { get; set; }

}

public class Sector

{

[Key, Required]

public int SectorID { get; set; }

[Required]

[Display(Name = "Sector Title")]

public string Title { get; set; }

[DefaultValue("No Description")]

[Display(Name = "Sector Description")]

public string Description { get; set; }

//public System.Drawing.Image Image { get; set; }

public virtual List<ProjectSector> ProjectSectors { get; set; }

}

and

public class ProjectSector

{

[Key, Required]

[Column(Order = 1)]

public int ProjectID { get; set; }

[Key, Required]

[Column(Order = 2)]

public int SectorID { get; set; }

public virtual Project Project { get; set; }

public virtual Sector Sector { get; set; }

}

Where a Project can be assigned to multiple sectors, and a Sector can contain multiple projects. I created the Model ProjectSector to represent this kind of relationship.

Now, during the Create action from the Controller Projects other than the default Editors for the Title and Description I would like to display some kind of Table with Buttons where you can select multiple (and leave selected status ON for the ones you clicked on), and once you send the Post request to create a new Instance of Project, I would like to have the controller automatically adding ProjectSectors for each Sector that the user chose the Project to be assigned to.

To do this, I'm passing the Sectors that are currently stored in my database through ViewData:

public ActionResult Create()

{

IEnumerable<Sector> Sectors = db.Sectors.ToList();

ViewData["Sectors"] = Sectors;

return View();

}

I was able to get this kind of layout using a table with Input buttons of type "Checkbox". But they're not interacting with the server in any way, and I would prefer actual buttons where I could just leave their Status as "Selected" and not update them every time I click on another one.

TL;DR: How can I create a view for a Project where I can show all Sectors from Database (Project has none during creation) and assign them to the Project Model by creating a new entry "ProjectSector" for each assignment between the Project and each Sector selected? Is it attainable using an Editor View?

Adding Objects to a Model (empty List) inside View using a "Many-to-Many" Relationship Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team