Asp.net MVC3的WebGrid和分页 [英] Asp.net Mvc3 webgrid and paging

查看:172
本文介绍了Asp.net MVC3的WebGrid和分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我努力学习Asp.net MVC。我知道,从形式的不同,我需要改变我大概的思维方式。我的问题是有关的WebGrid。当我添加的WebGrid到我的网页,并击中后搜索按钮,它呈现表,寻呼机等。但在寻呼机链接不张贴的形式,他们只是链接和我失去了我所有的表单数据。

控制器有两个指标的方法之一是get和另一种是职位。为了让我做什么,我只是在这种情况下,搜索类创建新的视图模型,并设置它来查看。对于我的方法后我抢我的视图模型做搜索和设置填充视图模型进行查看。

问题:的WebGrid呈现寻呼机的链接,以便将进入到指数GET,但因为它不是一个post请求我没有任何的表单字段填写和我的搜索将不提供同样的结果集

也许例如code能更好地解释它。

查看:

 <形式的行动=方法=后>欧空局没有:@ Html.TextBoxFor(X => x.Name)
YIL:@ Html.TextBoxFor(X => x.Year)<输入类型=提交值=搜索/><小时/>
@ ViewBag.Message
<小时/>@ {VAR电网=新的WebGrid(Model.Results,rowsPerPage:5);}@ grid.GetHtml(TABLESTYLE:表,htmlAttributes:新{ID =TBL})< /表及GT;

下面是我的控制器:搜索occures在索引Post方法,它具有只是我的ViewModel类

 私人ISearchContext _sc;    公共myController的(ISearchContext SC)
    {
        _dc = DC;
    }    //
    // GET:/达娃/    公众的ActionResult指数()
    {
        VAR搜索=新搜索();
        ViewBag.Message =;
        返回视图(搜索);
    }    [HttpPost]
    公众的ActionResult指数(搜索搜索)
    {        搜索SRES = _dc.SearchFromRepository(搜索);
        ViewBag.Message =的String.Format(计数:{0},sres.Results.Count);
        返回查看(SRES);
    }

搜索模型类是这样的:

 公共类搜索
{
    公众诠释年度{搞定;组; }
    公共字符串名称{;组; }
    公众的IList<项目>结果{搞定;组; }    公共搜索()
    {
        结果=新的List<项目>();
    }
}


解决方案

解决这个问题的方法之一是使用JavaScript和认购的任何寻呼机链接的点击事件,然后获取所需页面的价值,注入入窗体上的隐藏字段并提交表单到服务器,这样,其他两个值也会发送。

因此​​,通过增加你的搜索视图模型可为空整数属性启动和相应的隐藏字段成形成包含选定的页面数:

  @ Html.HiddenFor(X => x.Page,新{ID =页面})

然后,所有你需要的是一个小的JavaScript片段到页面认购寻呼机环节。点击事件:

  $(函数(){
    $('TFOOT一个')。点击(函数(){
        //当用户点击任何寻呼机链接的
        //尝试提取从链接的页号和
        //设置隐藏字段的值
        VAR页= this.href.match(/页=([0-9])+ /)[1];
        $('#页)VAL(页)。        //提交表单,以便调用POST操作
        //沿搜索标准(名称及年)一起传递
        //使用页的隐藏字段值索引动作
        $('形式')提交()。        //取消这是简单地重定向链接的默认操作
        //使用一个GET动词Index操作。
        返回false;
    });
});

I am trying to learn Asp.net mvc. I know its different from forms and i need to change my way of thinking probably. My problem is about webgrid . When i add webgrid to my page and hit search button with Post it renders table with pager and so on. But links on the pager is not posting form they are just links and i lost all my form's data.

Controller has two index methods one is for get and other is for post. For get i do nothing, I just create new viewmodel in this case Search class and set it to view. For my post method i grab my view model do search and set filled viewmodel to view.

problem : webgrid renders pager as links so it will enter to the Index for get but since it is not a post request i dont have any form fields filled and my search will not provide the very same result set.

Maybe example code can explain it better.

View:

<form action="" method="post">

Esas no : @Html.TextBoxFor(x=>x.Name)
Yil : @Html.TextBoxFor(x=>x.Year)

<input type="submit" value="Search" />

<hr />
@ViewBag.Message
<hr />

@{ var grid = new WebGrid(Model.Results,rowsPerPage:5);}

@grid.GetHtml(tableStyle:"table",htmlAttributes:new {id="tbl"} )

</form>

Here is My Controller: Search occures in Index Post method and it has just my viewmodel class.

    private ISearchContext _sc;

    public  MyController(ISearchContext sc)
    {
        _dc = dc;
    }

    //
    // GET: /Dava/

    public ActionResult Index()
    {
        var search = new Search();
        ViewBag.Message = "";
        return View(search);
    }

    [HttpPost]
    public ActionResult Index(Search search)
    {

        Search sres = _dc.SearchFromRepository(search);
        ViewBag.Message = String.Format("Count:{0} ",sres.Results.Count);
        return View(sres);
    }

Search model Class is like:

public class Search
{
    public int Year { get; set; }
    public string Name { get; set; }


    public IList<Item> Results { get; set; }

    public Search()
    {
        Results = new List<Item>();
    }
}

解决方案

One way to solve this issue is to use javascript and subscribe for the click event of any of the pager links and then fetch the value of the desired page, inject it into a hidden field on the form and submit the form to the server so that the other two values are also sent.

So start by adding a Page nullable integer property on your Search view model and a corresponding hidden field into the form which will contain the selected page number:

@Html.HiddenFor(x => x.Page, new { id = "page" })

Then all you need is a small javascript snippet into the page to subscribe for the .click event of the pager links:

$(function () {
    $('tfoot a').click(function () {
        // when the user clicks on any of the pager links
        // try to extract the page number from the link and
        // set the value of the hidden field
        var page = this.href.match(/page=([0-9])+/)[1];
        $('#page').val(page);

        // submit the form so that the POST action is invoked
        // passing along the search criteria (Name and Year) along
        // with the page hidden field value to the Index action
        $('form').submit();

        // cancel the default action of the link which is to simply redirect
        // to the Index action using a GET verb.
        return false;
    });
});

这篇关于Asp.net MVC3的WebGrid和分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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