c# - 如何在单独的页面中显示我的产品?

标签 c# html asp.net asp.net-mvc asp.net-mvc-5

所以我有一个基于 ASP.NET MVC 5 的购物车项目,其中一个要求是每页显示 8 个产品。我总共有 21 个产品。这就是我目前向他们展示的方式:

 public ActionResult Index()
    {
        String SQL = "SELECT ProductId, Products.CategoryId AS CategoryId, Name, ImageFileName, UnitCost"
            + ", SUBSTRING(Description, 1, 100) + '...' AS Description, isDownload, DownloadFileName "
            + "FROM Products INNER JOIN Categories ON Products.CategoryId = Categories.CategoryId ";

        String CategoryName = Request.QueryString.Get("CategoryName");
        if (CategoryName != null)
        {
            if (CategoryName.Length > 20 || CategoryName.IndexOf("'") > -1 || CategoryName.IndexOf("#") > -1)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            SQL += "WHERE CategoryName = @p0";
            ViewBag.CategoryName = CategoryName;
        }
        var products = db.Products.SqlQuery(SQL, CategoryName);
        return View(products.ToList());
    }

这是 cshtml:

@model IEnumerable<PiClub.Models.Product>
@{
    ViewBag.Title = "Shop";
}
@Styles.Render("~/Content/Site.css")
<h2>Shop</h2>

<table class="table">
<tr>
    <th>
        Name
    </th>
    <th>
        Image
    </th>
    <th>
        Price
    </th>
    <th>
        Description
    </th>
    <th>
        Category
    </th>
    <th></th>
</tr>

@foreach (var item in Model)
{
<tr>
    <td>
        @item.Name
    </td>
    <td>
        <img src="/Images/@item.ImageFileName" style="width:200px" />
    </td>

    <td style="text-align:right">
        @item.UnitCost
    </td>
    <td>
        @item.Description
    </td>
    <td>
        @item.Category.CategoryName
    </td>
    <td>
        <input type="button" value="Add to Cart" onclick="NavCart('@item.ProductId')" />
    </td>
    <td>
        <input type="button" value="Details" onclick="NavDetails('@item.ProductId')" />
    </td>
</tr>
}
</table>

<script type="text/javascript">
function NavDetails(ProductId) {
    window.location.replace("/Shop/Details?PrdouctId=" + ProductId);
}

function NavCart(ProductId) {
    window.location.replace("/OrderDetails/ShoppingCart?ProductId=" + ProductId);
}
</script>

我该怎么做呢?

最佳答案

在你的方法中放置两个参数,这将代表页码和每页的项目:

public ActionResult Index(int pageNumber, int itemsPerPage)

然后添加 Skip 和 Take 从数据库中获取数据的地方:

var products = db.Products.SqlQuery(SQL, CategoryName)
                          .Skip(pageNumber * itemsPerPage)
                          .Take(itemsPerPage);

然后通过url发送参数:

http://your-url/ControllerName/Index?pageNumber=2&itemsNumber=8

关于c# - 如何在单独的页面中显示我的产品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43406972/

相关文章:

c# - 如何从Azure Function更新服务总线消息?

c# - 帮助构建可测试的 API 客户端

c# - 为什么 UTF8Encoding.GetBytes 不发出字节顺序标记?

javascript - 如何使用 JavaScript 每秒向右移动 10 个像素并向下移动 10 个像素

c# - Application_End 未运行

c# - 为什么 NuGet 包管理器从项目文件中删除 SpecificVersion False

html - 居中对齐具有 float : left 的 div

javascript - 当我们滚动到 div 来到窗口中间位置时为我的 div 制作动画

jquery - 暂停和播放谷歌地图标记动画

javascript - ASP.NET:UpdateProgress 不适用于具有 ClientIDMode ="Static"的控件