如何获取插入对象的当前id并传递给另一个控制器

时间:2022-10-03 22:57:12

I have 2 view's: Auction and Item. In a first view I create Auction: 如何获取插入对象的当前id并传递给另一个控制器

我有2个视图:拍卖和物品。在第一个视图中,我创建了拍卖:

After clicked 'Create' it's redirect to Item View: 如何获取插入对象的当前id并传递给另一个控制器

点击“创建”后,它会重定向到项目视图:

How to send current id of inserted auction to do relation between auction and item. The finish result should be that actual item details has info about auction.

如何发送插入拍卖的当前ID以进行拍卖和项目之间的关系。完成结果应该是实际项目详细信息有关于拍卖的信息。

1 个解决方案

#1


1  

When you insert entity, DbContext assigns created entity id. Then you can pass that id to your second page in querystring parameter.

插入实体时,DbContext会分配创建的实体ID。然后你可以在querystring参数中将该id传递给你的第二页。

_context.Auctions.Add(auction);
_context.SaveChanges(); 
// auction.Id now contains database generated id
// pass it to second page, like this in asp.net mvc
return RedirectToAction("SecondPage", new { auctionId = auction.Id });

Second page:

第二页:

public ActionResult SecondPage(int auctionId) { ... }

#1


1  

When you insert entity, DbContext assigns created entity id. Then you can pass that id to your second page in querystring parameter.

插入实体时,DbContext会分配创建的实体ID。然后你可以在querystring参数中将该id传递给你的第二页。

_context.Auctions.Add(auction);
_context.SaveChanges(); 
// auction.Id now contains database generated id
// pass it to second page, like this in asp.net mvc
return RedirectToAction("SecondPage", new { auctionId = auction.Id });

Second page:

第二页:

public ActionResult SecondPage(int auctionId) { ... }