过滤DataBase以显示当前用户创建的顺序(应用程序用户)

时间:2017-05-23 15:53:41

标签: c# database filter asp.net-mvc-5

好的,我有一个MyOrders页面,当前显示订单数据库上的所有订单,我只需要显示当前应用程序用户使用的订单。

MyOrders cshtml page

@model IEnumerable<SpeedoModels.Models.Order>


<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
ViewBag.Title = "MyOrders";
 }

<h2>MyOrders</h2>



<h2>Orders Main</h2>

显示订单并允许以多种方式重新组织列表,即firstname

@if (User.IsInRole("Administrator"))
{
<p>
    @Html.ActionLink("Create New", "Create")
</p>
}
<table class="table table-striped table-hover">
<tr>
    <th>
        @Html.ActionLink("First Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
    </th>
    <th>
        Last Name
    </th>
    <th>
        @Html.ActionLink("Order Total", "Index", new { sortOrder = ViewBag.PriceSortParm, currentFilter = ViewBag.CurrentFilter })
    </th>
    <th>
        Address
    </th>
    <th>
        City
    </th>
    <th>
        State
    </th>
    <th>
        Postal Code
    </th>
    <th>
        Country
    </th>
    <th>
        Phone
    </th>
    <th>
        Email
    </th>
    <th></th>
</tr>

@foreach (var item in Model)
{
    <tr>

        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Total)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.City)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.State)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PostalCode)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Country)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Phone)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        @if (User.IsInRole("Administrator"))
        {
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.OrderId }) |
                @Html.ActionLink("Details", "Details", new { id = item.OrderId }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.OrderId })
            </td>

        }

        @Html.ActionLink("Cancel Order", "Delete", new { id = item.OrderId })


    </tr>
}


控制器中的MyOrders

public ActionResult MyOrders(string sortOrder, string currentFilter, string searchString, int? page)
    {
        ViewBag.CurrentSort = sortOrder;
        ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
        ViewBag.PriceSortParm = sortOrder == "Price" ? "price_desc" : "Price";

        if (searchString != null)
        {
            page = 1;
        }
        else
        {
            searchString = currentFilter;
        }

        ViewBag.CurrentFilter = searchString;

        var orders = from o in db.Orders
                     select o;
        //orders = orders.OrderByDescending(o = User.Identity.Name);

        if (!String.IsNullOrEmpty(searchString))
        {
            orders = orders.Where(s => s.FirstName.ToUpper().Contains(searchString.ToUpper())
                                   || s.LastName.ToUpper().Contains(searchString.ToUpper()));
        }
        switch (sortOrder)
        {
            case "name_desc":
                orders = orders.OrderByDescending(s => s.FirstName);
                break;
            case "Price":
                orders = orders.OrderBy(s => s.Total);
                break;
            case "price_desc":
                orders = orders.OrderByDescending(s => s.Total);
                break;
            default:  // Name ascending 
                orders = orders.OrderBy(s => s.FirstName);
                break;
        }


        return View(orders.ToList());

        //return View(await db.Orders.ToListAsync());
    }

1 个答案:

答案 0 :(得分:0)

假设您在FormsAuthenticationTicket名称属性中保存电子邮件。您可以按以下方式过滤订单:

var orders = from o in db.Orders
             where o.Email == User.Identity.Name
             select o;

检查您是否收到User.Identity.Name中的电子邮件。