Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Grid.mvc中更改页面大小_C#_Asp.net_Asp.net Mvc_Asp.net Mvc Controller_Grid.mvc - Fatal编程技术网

C# 如何在Grid.mvc中更改页面大小

C# 如何在Grid.mvc中更改页面大小,c#,asp.net,asp.net-mvc,asp.net-mvc-controller,grid.mvc,C#,Asp.net,Asp.net Mvc,Asp.net Mvc Controller,Grid.mvc,我有以下控制器: public ActionResult Grid() { schoolEntities db = new schoolEntities(); List<Student> result = db.Students.ToList(); // I can't use pagesizelist here, taken from the view ViewBag.p

我有以下控制器:

public ActionResult Grid()
        {
            schoolEntities db = new schoolEntities();
            List<Student> result = db.Students.ToList();
            // I can't use pagesizelist here, taken from the view
            ViewBag.pageSize = int.Parse(pagesizelist.SelectedValue);
            return View(result);
        }
public class HomeController : Controller
{
    public static readonly string viewNameWithGrid = "Grid";
    public static readonly int defaultPageSize = 10;

    private static readonly string SavedPageSizeSessionKey = "PageSizeKey";
    public int SavedPageSize
    {
        get
        {
            if (Session[SavedPageSizeSessionKey] == null)
                Session[SavedPageSizeSessionKey] = defaultPageSize;
            return (int)Session[SavedPageSizeSessionKey];
        }
        set { Session[SavedPageSizeSessionKey] = value; }
    }

    //The same as the Action name
    //return View(result);

    //Initial Load
    [HttpGet]
    public ActionResult Grid()
    {
        return GetViewWithGrid(SavedPageSize);
    }

    //Change Page Size
    [HttpPost]
    public ActionResult Grid(int? Page)
    {
        if (Page.HasValue)
            SavedPageSize = Page.Value;
        //Page = DropDownList.id
        return GetViewWithGrid(SavedPageSize);
    }

    ActionResult GetViewWithGrid(int pageSize)
    {
        schoolEntities db = new schoolEntities();
        List<Student> result = db.Students.ToList();

        //ViewBag.pageSize = int.Parse(pagesizelist.SelectedValue);
        ViewBag.pageSize = pageSize;
        return View(viewNameWithGrid, result);
    }
}
public ActionResult Grid()
{
schoolEntities db=新的schoolEntities();
列表结果=db.Students.ToList();
//我不能在这里使用pagesizelist,这是从视图中获取的
ViewBag.pageSize=int.Parse(pagesizelist.SelectedValue);
返回视图(结果);
}
和相关观点:

...
@Html.DropDownList("Page", new SelectList(new Dictionary<string, int> { { "10", 10 }, { "20", 20 }, { "50", 50 } }, "Key", "Value"), new { id = "pagesizelist" })

<div class="code-cut">
    @Html.Grid(Model).Columns(Columns =>
{
    Columns.Add(c => c.StudentID).Titled("Id").Filterable(true);
    Columns.Add(c => c.LastName).Titled("Last name").Filterable(true);
    Columns.Add(c => c.FirstName).Titled("First name").Filterable(true);
    Columns.Add(c => c.EnrollmentDate).Titled("Enrollment date").Filterable(true);
    Columns.Add()
...
}).WithPaging(ViewBag.pageSize).Sortable(true)
。。。
@DropDownList(“Page”,new SelectList(新字典{{“10”,10},{“20”,20},{“50”,50}),“Key”,“Value”),new{id=“pagesizelist”})
@Html.Grid(Model).Columns(Columns=>
{
Columns.Add(c=>c.StudentID).标题为(“Id”).Filterable(true);
Columns.Add(c=>c.LastName).Titled(“LastName”).Filterable(true);
Columns.Add(c=>c.FirstName).标题为(“FirstName”).Filterable(true);
列。添加(c=>c.EnrollmentDate)。标题为(“注册日期”)。可过滤(true);
Columns.Add()
...
}).WithPaging(ViewBag.pageSize)。可排序(true)
我想根据DropDownList中的更改动态设置
.WithPaging()
参数。

  • 将“页面”ddl包装在表单中

  • 订阅其客户端“onchange”。在那里提交表格

  • 在单独的操作方法中处理“更改大小”操作

  • 指定新的页面大小值并重新加载整个视图:

视图:

@model IEnumerable
@使用GridMvc.Html
函数onDdlPageChange(发送方){
$(“#formIdHere”).submit();
}
@使用(Html.BeginForm(“Grid”、“Home”、FormMethod.Post、new{id=“formIdHere”}))
{
@DropDownList(“页面”,新选择列表(新字典{{“10”,10},{“20”,20},{“50”,50}),“键”,“值”,ViewBag.pageSize),新{id=“pagesizelist”,onchange=“onDdlPageChange(this);”})
@Html.Grid(Model).Columns(Columns=>
{
Columns.Add(c=>c.StudentID).标题为(“Id”).Filterable(true);
Columns.Add(c=>c.LastName).Titled(“LastName”).Filterable(true);
Columns.Add(c=>c.FirstName).标题为(“FirstName”).Filterable(true);
列。添加(c=>c.EnrollmentDate)。标题为(“注册日期”)。可过滤(true);
//Columns.Add();
}).WithPaging(ViewBag.pageSize)。可排序(true)
}
控制器:

public ActionResult Grid()
        {
            schoolEntities db = new schoolEntities();
            List<Student> result = db.Students.ToList();
            // I can't use pagesizelist here, taken from the view
            ViewBag.pageSize = int.Parse(pagesizelist.SelectedValue);
            return View(result);
        }
public class HomeController : Controller
{
    public static readonly string viewNameWithGrid = "Grid";
    public static readonly int defaultPageSize = 10;

    private static readonly string SavedPageSizeSessionKey = "PageSizeKey";
    public int SavedPageSize
    {
        get
        {
            if (Session[SavedPageSizeSessionKey] == null)
                Session[SavedPageSizeSessionKey] = defaultPageSize;
            return (int)Session[SavedPageSizeSessionKey];
        }
        set { Session[SavedPageSizeSessionKey] = value; }
    }

    //The same as the Action name
    //return View(result);

    //Initial Load
    [HttpGet]
    public ActionResult Grid()
    {
        return GetViewWithGrid(SavedPageSize);
    }

    //Change Page Size
    [HttpPost]
    public ActionResult Grid(int? Page)
    {
        if (Page.HasValue)
            SavedPageSize = Page.Value;
        //Page = DropDownList.id
        return GetViewWithGrid(SavedPageSize);
    }

    ActionResult GetViewWithGrid(int pageSize)
    {
        schoolEntities db = new schoolEntities();
        List<Student> result = db.Students.ToList();

        //ViewBag.pageSize = int.Parse(pagesizelist.SelectedValue);
        ViewBag.pageSize = pageSize;
        return View(viewNameWithGrid, result);
    }
}
公共类HomeController:控制器
{
公共静态只读字符串viewNameWithGrid=“Grid”;
公共静态只读int defaultPageSize=10;
私有静态只读字符串SavedPageSizeSessionKey=“PageSizeKey”;
public int SavedPageSize
{
得到
{
if(会话[SavedPageSizeSessionKey]==null)
会话[SavedPageSizeSessionKey]=默认页面大小;
返回(int)会话[SavedPageSizeSessionKey];
}
设置{Session[SavedPageSizeSessionKey]=value;}
}
//与操作名称相同
//返回视图(结果);
//初始荷载
[HttpGet]
公共行动结果网格()
{
返回GetViewWithGrid(SavedPageSize);
}
//更改页面大小
[HttpPost]
公共行动结果网格(int?页)
{
如果(第页,HasValue)
SavedPageSize=Page.Value;
//Page=DropDownList.id
返回GetViewWithGrid(SavedPageSize);
}
ActionResult GetViewWithGrid(int pageSize)
{
schoolEntities db=新的schoolEntities();
列表结果=db.Students.ToList();
//ViewBag.pageSize=int.Parse(pagesizelist.SelectedValue);
ViewBag.pageSize=页面大小;
返回视图(viewNameWithGrid,result);
}
}

请参见以下内容:一旦更改页面大小,应始终在下次请求时应用。我已经更新了我的答案。