使用Json通过控制器将jqgrid绑定到实体框架 [英] Bind jqgrid with entity framework via a controller using Json

查看:71
本文介绍了使用Json通过控制器将jqgrid绑定到实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请给我解决使用json通过控制器绑定jqgrid和实体框架的解决方案,还需要jqgrid模板



谢谢...

解决方案

您好,



您可以参考以下代码以解决您遇到的问题。

这是jq Grid的模板,它是在视图中写的



 @(Html.Grid(  GridName
.SetRequestType(MvcJqGrid.Enums.RequestType.Post)
.SetJsonReader( new MvcJqGrid.DataReaders.JsonReader {Id = WId})
.SetCaption( 查看报告

.AddColumn( new 列( 名称).SetWidth( 100 )。SetLabel( Name) .SetSearchType(MvcJqGrid.Enums.Searchtype.Text))
.AddColumn( new 列( 描述)。SetWidth( 100 )。SetLabel( 描述)。SetSearchType(MvcJqGrid.Enums.Searchtype.Text))
.AddColumn( new 列( 状态)。SetWidth( 100 )。SetLabel( Status)。SetSearchType(MvcJqGrid.Enums .Searchtype.Text))
.AddColumn( new 列( 资源)。SetWidth( 100 )。Se tLabel( Resource)。SetSearchType(MvcJqGrid.Enums.Searchtype.Text))

.SetUrl(Url.Action( ViewName ControllerName))
.SetAutoWidth( true
.SetRowNum( 10
.SetSortName( WId
.SetViewRecords( true
.SetPager( pager
.SetLoadOnce( true )//对于客户侧排序分页启用

.SetSearchToolbar( true
.SetAutoWidth( true
.SetSea rchClearButton( true
.SetSearchToolbar( true
.SetSearchOnEnter( false
.SetScroll( false
.SetPgButtons( true
.SetSearchClearButton( true
.SetSearchToggleButton(真的





你可以在下面的控制器中使用它: -



  public  JsonResult ViewName(GridSettings gridSettings)
{
int totalRecords;
SREntities db = new SREntities();
totalRecords = db.VW_GetData.Count();
var jsonData = new
{
total = totalRecords / gridSettings .PageSize + 1
page = gridSettings.PageIndex,
records = totalRecords,
rows = db.VW_GetData.ToList()

};

return Json(jsonData,JsonRequestBehavior.AllowGet);
}





您必须为JQGridMvcJqGrid.dll添加DLL的引用并添加_Layout.cshtml中的jquery文件



 <   script     src   =  @ Url.Content( 〜/ Scripts / i18n / grid.locale-en.js )    type   =  text / javascript >  <   / script  >  
< script sr c = @ Url.Content( 〜/ Scripts / jquery.jqGrid.min.js) type = text / javascript > < / script >





请参考此链接获取更多帮助:

mvcjqgrid.skaele .it /



多数就完成!!


在控制器中编写你的逻辑以获得所需的数据然后你可以使用json.Net库将你的数据转换为json序列化对象并将其传递给你的视图。





这是只是我提供的方向,你可以在json.net框架上做更多的研究。你可以在visual studio中从nuget包安装json.net。





希望这会有所帮助。


嗨..



您可以参考我的文章。



http://www.codeproject.com/Articles/801895/Asp-Net-MVC -Entity-Framework-and-JQGrid-Demo-wit



这对初学者来说非常适合使用MVC,Entity框架和jqgrid的简单演示应用程序。

Please give me the solution for Binding jqgrid with entity framework via controller using json and also need jqgrid template

Thank you...

解决方案

Hi,

You can refer below code in order to resolve the issue that you faced.
This is the template for jq Grid which is written in view

@(Html.Grid("GridName")
 .SetRequestType(MvcJqGrid.Enums.RequestType.Post)
 .SetJsonReader(new MvcJqGrid.DataReaders.JsonReader { Id = "WId" })
 .SetCaption("View Report")

      .AddColumn(new Column("Name").SetWidth(100).SetLabel(" Name").SetSearchType(MvcJqGrid.Enums.Searchtype.Text))
.AddColumn(new Column("Description").SetWidth(100).SetLabel("Description").SetSearchType(MvcJqGrid.Enums.Searchtype.Text))
.AddColumn(new Column("Status").SetWidth(100).SetLabel("Status").SetSearchType(MvcJqGrid.Enums.Searchtype.Text))
     .AddColumn(new Column("Resource").SetWidth(100).SetLabel("Resource").SetSearchType(MvcJqGrid.Enums.Searchtype.Text))

    .SetUrl(Url.Action("ViewName", "ControllerName"))
    .SetAutoWidth(true)
    .SetRowNum(10)
    .SetSortName("WId")
    .SetViewRecords(true)
    .SetPager("pager")
    .SetLoadOnce(true)//for client side sorting and paging enabled
           
  .SetSearchToolbar(true)
  .SetAutoWidth(true)
  .SetSearchClearButton(true)
  .SetSearchToolbar(true)
  .SetSearchOnEnter(false)
  .SetScroll(false)
  .SetPgButtons(true)
  .SetSearchClearButton(true)
  .SetSearchToggleButton(true)
  )



You can use it in controller like below:-

public JsonResult ViewName(GridSettings gridSettings)
       {
           int totalRecords;
           SREntities db = new SREntities();
           totalRecords = db.VW_GetData.Count();
           var jsonData = new
           {
               total = totalRecords / gridSettings.PageSize + 1,
               page = gridSettings.PageIndex,
               records = totalRecords,
               rows = db.VW_GetData.ToList()

           };

           return Json(jsonData, JsonRequestBehavior.AllowGet);
       }



You have to add reference of a DLL for JQGrid "MvcJqGrid.dll " and also add jquery file in _Layout.cshtml

<script src="@Url.Content("~/Scripts/i18n/grid.locale-en.js")" type="text/javascript"></script>
   <script src="@Url.Content("~/Scripts/jquery.jqGrid.min.js")" type="text/javascript"></script>



Refer this link for furthur help:
mvcjqgrid.skaele.it/

Thats all done !!


well in controller write your logic to get the required data and then you can use json.Net library to convert your data to json serialize object and pass it to your view.


this is just a direction that i am providing, you can do some more research on json.net framework. you can install json.net from nuget package in the visual studio.


hope this will helpful.


Hi..

You can refer my article.

http://www.codeproject.com/Articles/801895/Asp-Net-MVC-Entity-Framework-and-JQGrid-Demo-wit

this will be good for beginner with a simple demo application using MVC, Entity framework and jqgrid.


这篇关于使用Json通过控制器将jqgrid绑定到实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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