如何按月分组Razor视图

时间:2017-03-24 03:08:30

标签: asp.net-mvc razor

我正在使用MVC剃刀视图

如何按我的索引视图分区在TrDate中的Month ??

我的索引控制器:

private InvoiceMasterEntities db = new InvoiceMasterEntities();

    public ActionResult Index(int compID, string searchBy, string search, int? page, string sortBy)
    {
        ViewBag.SortDateParam = string.IsNullOrEmpty(sortBy) ? "Date desc" : "";
        ViewBag.SortStatusParam = sortBy == "Status" ? "Status desc" : "Status";

        var detail = db.DetailPengirimen.AsQueryable();

        if (searchBy == "CustID")
        {
            detail = detail.Where(b => b.CustID.Contains(search) || search == null);
        }
        else
        {
            detail = detail.Where(a => a.Customer.Fullname.Contains(search) || search == null);
        }

        detail = detail.Where(a => a.CompID == compID);

        switch (sortBy)
        {
            case "Date desc":
                detail = detail.OrderByDescending(a => a.TrDate);
                break;
            case "Status desc":
                detail = detail.OrderByDescending(a => a.Status.Status1);
                break;
            case "Status":
                detail = detail.OrderBy(a => a.Status.Status1);
                break;
            default:
                detail = detail.OrderBy(a => a.TrDate);
                break;
        }

        return View(detail.ToPagedList(page ?? 1, 10));
    }

我的索引视图:

@using PagedList;
@using PagedList.Mvc;

@model IPagedList<InvoiceTracker.Models.DetailPengiriman>

<h2>Couriers List</h2>

<p>
@Html.ActionLink("Insert Data", "Create", new { compID =    Request.QueryString["compID"] })
</p>

<p>
@using (Html.BeginForm("Index", "DetailPengiriman", new { compID = Request.QueryString["compID"] }, FormMethod.Post))
{
    <b>Search By:</b>
    @Html.RadioButton("searchBy", "CustID", true) <text>CustomerID</text>
    @Html.RadioButton("searchBy", "Fullname") <text>Customer Name</text>

    <br />

    @Html.TextBox("search")
    <input type="submit" value="Search" />
}
 </p>

 <table class="table">
<tr>
    <th>
        @Html.ActionLink("Transaction Date", "Index", new
   {
       sortBy = ViewBag.SortDateParam,
       searchBy = Request.QueryString["searchBy"],
       search = Request["search"],
       compID = Request["compID"]
   })
    </th>
    <th>
        CustomerID
    </th>
    <th>
        Customer Name
    </th>
    <th>
        Invoice
    </th>
    <th>
        Nama Penerima
    </th>
    <th>
        Status Penerima
    </th>
    <th>
        @Html.ActionLink("Status", "Index", new
   {
       sortBy = ViewBag.SortStatusParam,
       searchBy = Request.QueryString["searchBy"],
       search = Request["search"],
       compID = Request["compID"]
   })
    </th>
    <th>
        Status Date
    </th>
    <th>
        Image
    </th>
    <th>Actions</th>
</tr>

 @foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.TrDate)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Customer.CustID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Customer.Fullname)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.InvoiceNo)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.NamaPenerima)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.StatusPenerima)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Status.Status1)
    </td>   
    <td>
        @Html.DisplayFor(modelItem => item.Status.StatDate)
    </td>      
    <td>           
        @if (item.Image.Image1 != null)
        {
            var base64 = Convert.ToBase64String(item.Image.Image1);
            var imgSrc = ($"data:image/png;base64,{base64}");
            <img src="@imgSrc" width="50" height="50" 
            onMouseOut="zoomtoggle('50','50px','50px','50px',this);"  
                  onMouseOver="zoomtoggle('400px','200px','400px','200px',this);"/>
        }
        else
        {
            <text> No Image Available</text>
        }
    </td>        
    <td>
        @Html.ActionLink("Edit", "Edit", new
   {
       id = item.TransID,
       custID = item.CustID,
       statID = item.StatusID,
       imgID = item.ImageID,
       invoNo = item.InvoiceNo
   }) |
        @Html.ActionLink("Delete", "Delete", new
   {
       id = item.TransID,
       custID = item.CustID,
       statID = item.StatusID,
       imgID = item.ImageID,
       invoNo = item.InvoiceNo
   })
    </td>
</tr>
 }

 </table>

 @Html.PagedListPager(Model, page => Url.Action("Index", new
 {
page,
searchBy = Request.QueryString["searchBy"],
search = Request.QueryString["search"],
sortBy = Request["sortBy"],
compID = Request["compID"]
 }),
new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded })

我希望视图在TrDate属性中按月列出数据库组中的数据 TrDate数据来自datetime.now TrDate数据类型是DateTime

0 个答案:

没有答案