排序上的C#MVC webgrid仅渲染没有父级的部分视图 [英] C# MVC webgrid on sort only renders partial view without parent

查看:54
本文介绍了排序上的C#MVC webgrid仅渲染没有父级的部分视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC和学习新手。



我只有一页,我有一个包含webgrid的局部视图。如果我对它进行排序,我可以使用下拉列表进行过滤。但是,如果我过滤,那么排序,部分视图是屏幕上唯一没有父索引,没有CSS等的东西。



这是我的索引Views / Home文件夹中包含的.cshtml(我必须删除这里显示的元素的一些括号):



@

I am new to MVC and learning.

I have one page only where I have a partial view containing a webgrid. If I sort it works, and I can filter using a drop down list. HOWEVER, if I filter, THEN sort, the partial view is the only thing that appears on screen without the parent Index, no CSS, etc.

This is my Index.cshtml contained in the Views/Home folder (I had to remove some brackets for the elements to show here):

@

model IEnumerable<FHApp.Models.Bulletins>

@{
    ViewData["Title"] = "Home";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Welcome</h2>

<p>Please note current bulletin items:</p>

@Html.DropDownList("lstBulletins", (SelectList)ViewBag.BulletinTypes)

<br /><br />

<div id="BulletinViewGrid" style="width: 75%">
    @Html.Partial("Bulletins", Model)
</div>

<script type="text/javascript">
    $(document).ready(function () {
        //Dropdownlist Selectedchange event
        $("#lstBulletins").change(function () {
            var sBulletinType = $(this).val();
            var url = '@Url.Action("Filter")';
            $.get(url, { sBulletinType: $(this).val() }, function (result) {
                debugger;
                $('#BulletinViewGrid').html(result);
            });
        });
        return false;
    });

</script>





------- -----------------------



Views / Home文件夹中包含的My Bulletins.cshtml :





------------------------------

My Bulletins.cshtml contained in the Views/Home folder:

@{
    WebGrid gridBulletins = new WebGrid(source: Model, canSort: true, defaultSort: "DatePosted");
    if (Request.QueryString[gridBulletins.SortDirectionFieldName].IsEmpty())
    {
        gridBulletins.SortDirection = SortDirection.Descending;
    }
}

@gridBulletins.GetHtml(
    tableStyle: "webgrid-table",
    headerStyle: "webgrid-header",
    footerStyle: "webgrid-footer",
    alternatingRowStyle: "webgrid-alternating-row",
    selectedRowStyle: "webgrid-selected-row",
    rowStyle: "webgrid-row-style",
    mode: WebGridPagerModes.All,
    columns: new[] {
        gridBulletins.Column("DatePosted", @Helpers.Sorter("DatePosted", "Date Posted", gridBulletins), format: (item) => string.Format("{0:MMM d, yyyy}", item.DatePosted), style: "webgrid-column-nowrap"),
        gridBulletins.Column("Bulletin",  @Helpers.Sorter("Bulletin", "Bulletin", gridBulletins), format: (item) => {
            string Bulletin = item.Bulletin ?? "";
            return new HtmlString(Bulletin.Replace("\r\n", "<br/>"));
            })
    }
)





----------- -------------------



我的HomeController.cs包含在Controllers文件夹中:





------------------------------

My HomeController.cs contained in the Controllers folder:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FHApp.Models;

public class HomeController : Controller
{
    private FHDataEntities db = new FHDataEntities();

    public ActionResult Index()
    {
        ViewBag.BulletinTypes = new SelectList(
            new List<SelectListItem> {
            new SelectListItem { Text="All", Value = ""}, 
            new SelectListItem { Text="Memos", Value = "M"},
            new SelectListItem { Text="Trainings", Value = "T"},
            new SelectListItem { Text="Postings", Value = "P"}
            }, "Value", "Text");

        DateTime dtCurrentDate = DateTime.Now;

        var Bulletins = db.Bulletins.ToList();

        Bulletins = Bulletins.Where(s => s.ExpiryDate >= dtCurrentDate || !s.ExpiryDate.HasValue).ToList();

        return View(Bulletins);
    }

    public ActionResult Filter(string sBulletinType)
    {
        DateTime dtCurrentDate = DateTime.Now;

        var Bulletins = db.Bulletins.ToList();

        if (!string.IsNullOrEmpty(sBulletinType))
        {
            Bulletins = Bulletins.Where(s => (s.ExpiryDate >= dtCurrentDate || !s.ExpiryDate.HasValue)
            && s.BulletinType.ToLower().Contains(sBulletinType.ToLower())).ToList();
        }

        return PartialView("Bulletins", Bulletins);
    }
}





----------------- -----------------------------



Helpers.cshtml代码在App_Code文件夹中:





----------------------------------------------

The Helpers.cshtml code in the App_Code folder:

@functions {
    public static string Sorter(string columnName, string columnHeader, WebGrid grid)
    {
        return string.Format("{0} {1}", columnHeader, grid.SortColumn == columnName ?
            grid.SortDirection == SortDirection.Ascending ? "▲" :
            "▼" : string.Empty);
    }
}





我的尝试:



我无休止地用Google搜索。我不知道有谁编写MVC来寻求帮助。



What I have tried:

I have googled endlessly. I do not know anyone personally who codes MVC to ask for help.

推荐答案

document )。ready ( function (){
// 下拉列表Selectedchange事件
(document).ready(function () { //Dropdownlist Selectedchange event


#lstBulletins ).change( function (){
var sBulletinType =
("#lstBulletins").change(function () { var sBulletinType =


this )。val();
var url = ' @ Url.Action(过滤器)';
(this).val(); var url = '@Url.Action("Filter")';


这篇关于排序上的C#MVC webgrid仅渲染没有父级的部分视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆