[英]Update relational DB data based on MVC actionresult (Entity Framework)
I'm trying to update a table within my DB to change a particular relationship based on a user selecting a card, and then click a button to send that card to that deck, see below: 我正在尝试更新数据库中的表,以根据用户选择一张卡来更改特定关系,然后单击一个按钮将该卡发送到该卡组,如下所示:
My Models: 我的模特:
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; }
}
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; }
}
My Controller action: 我的控制器动作:
public ActionResult AddToDeck(int id)
{
var cardInDb = _context.Cards.Single(c => c.Id == id);
var deckInDb = _context.Decks.Single(d => d.id == id);
foreach (var c in cardInDb.Deck)
{
cardInDb.Id = deckInDb.id;
}
_context.SaveChanges();
return View("Index");
}
My View: 我的观点:
@using YGOBuilder.Controllers
@model YGOBuilder.Models.Card
<div>
<h4>Card</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Atk)
</dt>
<dd>
@Html.DisplayFor(model => model.Atk)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Def)
</dt>
<dd>
@Html.DisplayFor(model => model.Def)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Desc)
</dt>
<dd>
@Html.DisplayFor(model => model.Desc)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Level)
</dt>
<dd>
@Html.DisplayFor(model => model.Level)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Type)
</dt>
<dd>
@Html.DisplayFor(model => model.Type)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Attribute)
</dt>
<dd>
@Html.DisplayFor(model => model.Attribute)
</dd>
@foreach (var item in Model.Card_Images)
{
<td>
<img src=@item.image_url height="300" width="200" style="margin: 2px">
</td>
}
</dl>
</div>
<p>
@Html.ActionLink("Back to List", "Index")
</p>
The DB Table I'm trying to manipulate looks as follows there is no model attached to this, this was created by Entity Framework for a many to many relationship: 我尝试操作的数据库表如下所示,没有附加任何模型,它是由实体框架为多对多关系创建的:
DeckCards
(key sign) Deck_id
(key sign) Card_id
I want to somehow display a list of all current decks in the view above, would I need to create a viewModel for this? 我想以某种方式在上面的视图中显示所有当前牌组的列表,是否需要为此创建一个viewModel?
Also, what is the best approach for when the user is viewing a card with an id of 1, they then want to update the DeckCards table to pair that card with a deck of id 3, eg 另外,当用户查看ID为1的卡片时,最好的方法是什么,然后他们想要更新DeckCards表以将该卡片与ID为3的卡片组配对,例如
Deck_Id Card_Id
3 1
I think your controller action is going to make all the cards in the deck the same. 我认为您的控制器操作将使卡片组中的所有卡相同。 You have the following: 您具有以下内容:
foreach (var c in cardInDb.Deck)
{
cardInDb.Id = deckInDb.id;
}
Instead, try: 相反,请尝试:
deckInDb.Card.Add(cardInDb);
cardInDb.Deck.Add(deckInDb);
Unless I'm misunderstanding your goal, when you call SaveChanges(), the DB should get the appropriate updates. 除非我误解了您的目标,否则当您调用SaveChanges()时,数据库应该获取适当的更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.