C# 如何使用ASP.NETMVC将商品从商店添加到购物车?

C# 如何使用ASP.NETMVC将商品从商店添加到购物车?,c#,asp.net,asp.net-mvc,entity-framework,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,我正在尝试制作一个网络商店应用程序,在本例中,我以视频游戏为例。我为一个名为VideoGame.cs的视频游戏创建了一个模型类,它位于我的ASP.NET MVC项目的Models文件夹中。这个类看起来像: namespace VideoGameStore.Models { public class VideoGame { public int Id { get; set; } public string Title { get; set; }

我正在尝试制作一个网络商店应用程序,在本例中,我以视频游戏为例。我为一个名为
VideoGame.cs
的视频游戏创建了一个模型类,它位于我的ASP.NET MVC项目的
Models
文件夹中。这个类看起来像:

namespace VideoGameStore.Models
{
    public class VideoGame
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
    }

    public class VideoGameDBContext : DbContext
    {
        public DbSet<VideoGame> VideoGames { get; set; }
    }
}
最后,我将有一个视图,在该视图中,项目将向用户显示商店中可用的视频游戏集合(如果用户是管理员,也可以在此视图中管理该集合)。位于
Views/VideoGames
文件夹中名为
Index.cshtml
的视图如下所示:

@model PagedList.IPagedList<VideoGameStore.Models.VideoGame>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "Video Games Store";
}

<h2>Video Games Store</h2>

@if (User.Identity.IsAuthenticated)
{
    if (User.IsInRole("Admin"))
    {
        <p>
            @Html.ActionLink("Create New", "Create")
        </p>
    }
}

@using(Html.BeginForm("Index", "VideoGames", FormMethod.Get))
{
    <p>
        @Html.TextBox("searchString", ViewBag.CurrentFilter as string)
        <input type="submit" value="Search" />
    </p>
}

<div class="row">
    @foreach (var item in Model) {
        <div class="col-md-3">
            @Html.DisplayFor(modelItem => item.Title)<br />
            @Html.DisplayFor(modelItem => item.Genre)<br />
            &euro;@Html.DisplayFor(modelItem => item.Price)<br />
            @if (User.Identity.IsAuthenticated)
            {
                if(User.IsInRole("Admin"))
                {
                    @Html.ActionLink("Edit", "Edit", new { id = item.Id }) <text> | </text>
                }
            }
            @Html.ActionLink("Details", "Details", new { id=item.Id })
            @if (User.Identity.IsAuthenticated)
            {
                if (User.IsInRole("Admin"))
                {
                    <text> | </text> @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                }
            }
            <br />
            @Html.ActionLink("Add to cart", "AddToCart", new { controller = "ShoppingCart", id = item.Id })
        </div>
    }
</div>
<div class="row">
    <div class="col-md-12">
        Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

        @Html.PagedListPager(Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
    </div>
</div>

我一直在读到,在MVC中使用session来制作购物车并不是解决这个问题的理想方法。我是否必须将ShoppingCart模型添加到我的项目中,并将购物车数据存储在数据库中(如果是,我如何使其看起来像这样)?我还读到,我不希望用户在HTTP GET请求上存储数据,但我不希望用户不得不在另一个页面上再次发布将商品添加到购物车的决定。如何调整我的
ShoppingCartController.cs

您使用AJAX
POST/ShoppingCart/AddToCart/1234
,这样用户就不会离开当前页面。此操作将购物“会话”保存到数据库中,直到结帐为止
@model PagedList.IPagedList<VideoGameStore.Models.VideoGame>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "Video Games Store";
}

<h2>Video Games Store</h2>

@if (User.Identity.IsAuthenticated)
{
    if (User.IsInRole("Admin"))
    {
        <p>
            @Html.ActionLink("Create New", "Create")
        </p>
    }
}

@using(Html.BeginForm("Index", "VideoGames", FormMethod.Get))
{
    <p>
        @Html.TextBox("searchString", ViewBag.CurrentFilter as string)
        <input type="submit" value="Search" />
    </p>
}

<div class="row">
    @foreach (var item in Model) {
        <div class="col-md-3">
            @Html.DisplayFor(modelItem => item.Title)<br />
            @Html.DisplayFor(modelItem => item.Genre)<br />
            &euro;@Html.DisplayFor(modelItem => item.Price)<br />
            @if (User.Identity.IsAuthenticated)
            {
                if(User.IsInRole("Admin"))
                {
                    @Html.ActionLink("Edit", "Edit", new { id = item.Id }) <text> | </text>
                }
            }
            @Html.ActionLink("Details", "Details", new { id=item.Id })
            @if (User.Identity.IsAuthenticated)
            {
                if (User.IsInRole("Admin"))
                {
                    <text> | </text> @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                }
            }
            <br />
            @Html.ActionLink("Add to cart", "AddToCart", new { controller = "ShoppingCart", id = item.Id })
        </div>
    }
</div>
<div class="row">
    <div class="col-md-12">
        Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

        @Html.PagedListPager(Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
    </div>
</div>
namespace VideoGameStore.Controllers
{
    public class ShoppingCartController : Controller
    {
        // GET: ShoppingCart
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult AddToCart(int Id)
        {
            //What code could I add here?
            return RedirectToAction("Index");
        }
    }
}