Mitchelln11
Active member
- Joined
- Apr 10, 2020
- Messages
- 39
- Programming Experience
- Beginner
I'm having issues trying to make a wishlist.
I have a database with records of National Parks.
I also have a database with records of Hikers.
I have a button on the Park details view page that is supposed to redirect to an action.
The button is passing the park Id, so I'm already getting that through the parameter.
I created a method to find the logged in user and the current park, but I'm stuck at trying to add items to my database.
I'm not sure what my Junction table is supposed to look like.
At first, I had it set up like the following, but was faced with error related to looping through a single object:
A previous post of mine led me to believe that I should try the following:
Any hiker should be able to add any/all parks to their wishlist.
Any park can exist in in any/all hikers wishlists, so that should be a many-to-many relationship (correct me if that's wrong)
Where am I going wrong?
Current error:
I feel like I should be using IEnumerable, or a list on my action, but I'm not quite sure how to implement that.
I have a database with records of National Parks.
I also have a database with records of Hikers.
I have a button on the Park details view page that is supposed to redirect to an action.
C#:
@Html.ActionLink("Add Park to Wish List", "AddParkToWishList", "Hiker", new { @id = Model.ParkId }, new { @class = "btn btn-default" })
The button is passing the park Id, so I'm already getting that through the parameter.
I created a method to find the logged in user and the current park, but I'm stuck at trying to add items to my database.
C#:
public async Task<ActionResult> AddParkToWishList(int id)
{
string currentUser = FindRegisteredUserId(); // AspNetUser Id
var currentHiker = _context.Hikers.Where(h => h.ApplicationId == currentUser); // Match against Hikers to find logged in user
// Find current park
var currentPark = _context.Parks.Where(p => p.ParkId == id).FirstOrDefault();
HikerParkWishlistViewModel wishlist = new HikerParkWishlistViewModel()
{
wishlist.Hiker = currentHiker;
// STUCK HERE. CAN'T ADD ANYTHING TO JUNCTION TABLE
}
await _context.SaveChangesAsync();
return RedirectToAction("Details");
}
I'm not sure what my Junction table is supposed to look like.
At first, I had it set up like the following, but was faced with error related to looping through a single object:
C#:
public class HikerParkWishlist // Junction table
{
[Key]
public int HikerParkWishlistId { get; set; }
[ForeignKey("Hiker")]
public int HikerId { get; set; }
public Hiker Hiker { get; set; }
[ForeignKey("Park")]
public int ParkId { get; set; }
public string ParkName { get; set; }
public Park Park { get; set; }
}
A previous post of mine led me to believe that I should try the following:
C#:
public class HikerParkWishlist
{
[Key]
public int HikerParkWishlistId { get; set; }
[ForeignKey("Hiker")]
public IEnumerable<Hiker> Hiker { get; set; }
[ForeignKey("Park")]
public IEnumerable<Park> Parks { get; set; }
}
Any hiker should be able to add any/all parks to their wishlist.
Any park can exist in in any/all hikers wishlists, so that should be a many-to-many relationship (correct me if that's wrong)
Where am I going wrong?
Current error:
C#:
CS1922 C# Cannot initialize type with a collection initializer because it does not implement 'System.Collections.IEnumerable'
I feel like I should be using IEnumerable, or a list on my action, but I'm not quite sure how to implement that.
Last edited: