I'm trying to execute some SQL query from my controller action that takes in the VALES () Parameters based on ids in the view, some code to better explain:
My Controller, based on what i found online :
public ActionResult Add(int id)
{
Entitiesdb db = new Entitiesdb();//not sure if this be deck or cards?
string query = $"INSERT INTO DeckCards (Card_Id, Deck_id) Values ({cardId},{deckId});";
db.Database.ExecuteSqlCommand(query, @id);
return View("Index");
}
My View button that calls the Add Method in the controller:
@foreach (var d in Model.Decks)
{
<ul class="nav navbar-nav navbar-right">
@Html.ActionLink($"Add to: {d.Name} ", "Add", new { cardId = Model.Cards.Id, deckId = d.id })
</ul>
}
My models:
public class Deck
{
public int id { get; set; }
public string Name { get; set; }
public string Notes { get; set; }
[DisplayName("Card")]
public virtual List<Card> Card { get; set; }
}
public class Card
{
public int Id { get; set; }
public string Name { get; set; }
public int? Atk { get; set; }
public int? Def { get; set; }
public string Desc {get; set;}
public int? Level { get; set; }
public string Type { get; set; }
public string Attribute { get; set; }
[DisplayName("Image")]
public virtual List<Image> Card_Images { get; set; }
public virtual List<Deck> Deck { get; set; }
}
DeckCards, set its properties, then save it using the EFDbContext.SaveChanges()method....... what's the point of having EF included, and then using "raw" SQL to do stuff?!?!