Asp.net web api 如何在Asp.Net中跨页面传递模型ID
我想将Model.Game ID从详细信息页面传递到另一个查看页面,即购物车。 我的做法是在app-route-ID中插入Asp.net web api 如何在Asp.Net中跨页面传递模型ID,asp.net-web-api,Asp.net Web Api,我想将Model.Game ID从详细信息页面传递到另一个查看页面,即购物车。 我的做法是在app-route-ID中插入@Html.DisplayFor(model=>model.Game.ID)。这样做合适吗 Details.cshtml @page "{id:int}" @model GameSanctuary.Pages.Games.DetailsModel @{ ViewData["Title"] = "Details"; } <h2>Details</h
@Html.DisplayFor(model=>model.Game.ID)
。这样做合适吗
Details.cshtml
@page "{id:int}"
@model GameSanctuary.Pages.Games.DetailsModel
@{
ViewData["Title"] = "Details";
}
<h2>Details</h2>
<div>
<h4>Game</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Game.G_Description)
</dt>
<dd>
@Html.DisplayFor(model => model.Game.G_Description)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Game.G_Genre)
</dt>
<dd>
@Html.DisplayFor(model => model.Game.G_Genre)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Game.G_Price)
</dt>
<dd>
@Html.DisplayFor(model => model.Game.G_Price)
</dd>
</dl>
</div>
<div>
<a asp-page="./Index">Back to List</a>
<a asp-page="./Cart" asp-page-handler="buynow" class="btn" asp-route-id="@Html.DisplayFor(model => model.Game.ID)">Add to Cart</a>
</div>
@page“{id:int}”
@模型gamesactionary.Pages.Games.details模型
@{
ViewData[“标题”]=“详细信息”;
}
细节
游戏
@DisplayNameFor(model=>model.Game.G_说明)
@DisplayFor(model=>model.Game.G_说明)
@DisplayNameFor(model=>model.Game.G_流派)
@DisplayFor(model=>model.Game.G_流派)
@DisplayNameFor(model=>model.Game.G_Price)
@DisplayFor(model=>model.Game.G_Price)
返回列表
添加到购物车
Cart.cshtml.cs
public async Task<IActionResult> OnGetBuyNow(int id)
{
cart = SessionHelper.GetObjectFromJson<List<Item>>(HttpContext.Session, "cart");
if (cart == null)
{
cart = new List<Item>();
cart.Add(new Item
{
Game = await _context.Game.FirstOrDefaultAsync(m => m.ID == id)
});
SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
}
return RedirectToPage("Cart");
}
公共异步任务OnGetBuyNow(int-id)
{
cart=SessionHelper.GetObjectFromJson(HttpContext.Session,“cart”);
如果(购物车==null)
{
购物车=新列表();
购物车。添加(新项目)
{
Game=wait\u context.Game.FirstOrDefaultAsync(m=>m.ID==ID)
});
SetObjectAsJson(HttpContext.Session,“cart”,cart);
}
返回Topage(“购物车”);
}
我在
OnGetBuyNow
中检索到的id值为0,但根据Details.cshtml
中的model.Game.id,预期值应为1。这两者之间缺少什么吗?不,这不合适
Html.DisplayFor()
呈现与您在其上使用的任何类型相匹配的显示模板。那不是你想要的。您需要的是一个简单的数字,它可以像这样直接注入:
asp-route-id="@Model.Game.ID"
我会从那里开始,看看这是否能满足您的需求谢谢,这才是我真正想要的