ورود

View Full Version : مشکل با نمایش داده از دیتابیس در گرید kendo توسط AjaxBinfing



rayangostar_co
چهارشنبه 07 آبان 1393, 11:47 صبح
با سلام خدمت دوستان

تو کنترلرم اینو نوشتم

public JsonResult Read_Phone([DataSourceRequest]DataSourceRequest request) {
using (var db = new PhoneEntity())
{
IQueryable<Person> p = db.Persons;
DataSourceResult result = p.ToDataSourceResult(request);




return Json(result,JsonRequestBehavior.AllowGet);
}
}

ویو هم به این صورت هستش

@{ ViewBag.Title = "Read_Phone";
Layout = "~/Views/Shared/_Layout.cshtml";
}


@(Html.Kendo().Grid<KendoUIMvcGrid.Models.Person>()
.Name("grid")
.Columns(columns =>
{
// Create a column bound to the ProductID property
columns.Bound(p => p.Id);
// Create a column bound to the ProductName property
columns.Bound(p => p.Name);
// Create a column bound to the UnitsInStock property
columns.Bound(p => p.Mobile);
})
.DataSource(dataSource => dataSource
.Ajax()
.Read(read =>
read.Action("Read_Phone", "Home").Type(HttpVerbs.Get)) // Set the action method which will return the data in JSON format
)
.Pageable() // Enable paging
.Sortable() // Enable sorting


)

وقتی که برنامه و اجرا میکنم . مرورگر داده برگشتی json رو به حالت download فایل میاره و از کاربر میخواد که save یا کنسل کنه.
چرا تو گرید لود نمیشه . مشکل کجاست. ممنون میشم اگه دوستان راهنمایی کنند.

mehdin69
چهارشنبه 07 آبان 1393, 12:33 عصر
اوکی
از View که داری از تمام Prop هایی که داره بیا یه کپی بگیر و یه Modelدیگه بساز توی Folder Models
اون رو به عنوان View Model صدا بزن
حالا اینطوری توی کنترلر عمل کن
فکر کن من یه مدل داشتم به نام Contractor که اینطوری بوده



//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.ComponentModel.DataAnnotations;

namespace CIMS.Models
{
using System;
using System.Collections.Generic;

public partial class Contractor
{
public Contractor()
{
this.Personel = new HashSet<Personel>();
this.MIV = new HashSet<MIV>();
}
[Key]
[Required]
public int ID_Contractor { get; set; }
[ScaffoldColumn(false)]
[DisplayFormat(ApplyFormatInEditMode = false)]
public int Project_ID { get; set; }
[Display(Name = "Contractor")]
[Required]
public string Description { get; set; }
[ScaffoldColumn(false)]
[DisplayFormat(ApplyFormatInEditMode = false)]
public System.Guid Guid_Contractor { get; set; }
[ScaffoldColumn(false)]
[DisplayFormat(ApplyFormatInEditMode = false)]
public System.DateTime CreateDate { get; set; }
[ScaffoldColumn(false)]
[DisplayFormat(ApplyFormatInEditMode = false)]
public System.DateTime LastUpdateDate { get; set; }

public virtual Project Project { get; set; }
public virtual ICollection<Personel> Personel { get; set; }
public virtual ICollection<MIV> MIV { get; set; }
}
}




حالا View Model به شکل زیر میشه


using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace CIMS.Models
{
public class ContractorViewModel
{

public int ID_Contractor { get; set; }
public string Description { get; set; }
public int Project_ID { get; set; }
}
}


حالا توی کنترلر اینطوری بنویس


public ActionResult IndexData([DataSourceRequest]DataSourceRequest request)
{
return Json(GetData().ToDataSourceResult(request));
}

private static IEnumerable<ContractorViewModel> GetData()
{
CIMSEntities db = new CIMSEntities();
return (from contractor in db.Contractor
select new ContractorViewModel()
{
ID_Contractor = contractor.ID_Contractor,
Description = contractor.Description
});
}


حالا توی View بیا این رو بنویس


@(Html.Kendo().Grid<CIMS.Models.ContractorViewModel>()
.Name("grid_Contractor")

.Columns(columns =>
{
columns.Bound(p => p.Description)
.ClientFooterTemplate("Total Count: #=count#")
.ClientGroupFooterTemplate("Count: #=count#");
})

.Groupable()
.Sortable(sort => sort.SortMode(GridSortMode.MultipleColumn))
.Scrollable(scroll => scroll
.Enabled(true)
.Height(450)
)
.Filterable()
.Selectable(selectable => selectable.Mode(GridSelectionMode.Multiple))
.Resizable(resize => resize.Columns(true))
.Pageable(page => page.ButtonCount(20)
.Info(true)
.Refresh(true)
.PageSizes(true)
.Numeric(true)
.PreviousNext(true)
.Input(true)
)
.Events(e => e.DataBound("onRowBound"))
.HtmlAttributes(new { style = "height:450px;" })
.RowAction(row => row.HtmlAttributes.Add("data-id", row.DataItem.ID_Contractor))
.DataSource(dataSource => dataSource
.Ajax()
.Aggregates(aggregates =>
{
aggregates.Add(p => p.Description).Min().Max().Count();
//aggregates.Add(p => p.UnitsOnOrder).Average();
//aggregates.Add(p => p.ProductName).Count();
//aggregates.Add(p => p.UnitPrice).Sum();
})
.PageSize(20)
.Read(read => read.Action("IndexData", "Contractor"))
)
.ColumnMenu()

rayangostar_co
چهارشنبه 07 آبان 1393, 12:59 عصر
یعنی تنها تفاوتش تو اینکه یه viewModel از کلاس پایه بسازی؟
درضمن مهندس نباید از JsonRequestBehavior.AllowGet به عنوان پارامتر return استفاده کنی.
فکر کنم مرورگر اجاز اجرا نده و با خطا مواجه شم.

mehdin69
چهارشنبه 07 آبان 1393, 13:09 عصر
الان اینی که واست فرستادم داره کار میکنه
تنها تفاوتشم همینه که گفتی

rayangostar_co
چهارشنبه 07 آبان 1393, 13:15 عصر
حدسم درست بود وقتی که از
return Json(GetData().ToDataSourceResult(request));
به جای

return Json(GetData().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
استفاده کنی خطای زیر میاد.
This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.

rayangostar_co
چهارشنبه 07 آبان 1393, 13:27 عصر
مهندس چطور میشه این خطا رو برطرف کرد؟؟
اگه با پروپرتی JsonRequestBehavior.AllowGet خطا رو برطرف کنم همون مشکل پیغام ذخیره فایل به جای لود تو گرید پیش میاد.
از پاسخگویی شما ممنونم