我正在使用MVC和Kendo Grid。我面临的问题是,我们为所有主表提供了通用功能,并将结果集存储到基于dealersystemId的缓存中。 有不同的dealerystemId,因此数据将存储在每个dealerystemId的缓存中。 因此,无论何时主表所需的调用,它总是使用缓存而不是调用主表,如果缓存中没有特定主表的数据,那么它只会访问数据库,否则从缓存中读取。 我们有一个屏幕,显示来自缓存的所有数据,用户将能够删除每个dealerystemid的存储缓存。因此,当用户点击dealerystemId的“clearcache”按钮时,将发生Jquery ajax并从缓存中删除经销商系统的数据,并且缓存中的剩余数据将显示在Kendo网格中。 清除缓存数据代码的功能,如下所述,
public List<SystemPreferenceModel> ClearAllSysPreference(string dealerSystemID,bool selfClear = true)
{
try
{
List<SystemPreferenceModel> systemPreferenceModel = null;
List<mtSystemPreference> appSystemPrefModel = null;
appSystemPrefModel = (List<mtSystemPreference>)CachingProvider.Instance.GetItem("GetAllSysPreference2");
if (appSystemPrefModel != null)
{
systemPreferenceModel = new List<SystemPreferenceModel>();
IGenericDataRepository<mtSystemPreference> appSysPrefRepository = new GenericDataRepository<mtSystemPreference>(UserModelSession);
appSystemPrefModel.RemoveAll(x => x.dealerSystemId == Convert.ToInt64(dealerSystemID));
systemPreferenceModel = GetSystemPreferenceData(appSystemPrefModel);
CachingProvider.Instance.AddItem("GetAllSysPreference2", appSystemPrefModel, string.Empty);
}
var appSettings = ConfigurationManager.AppSettings;
string server1 = appSettings["Server1"];
string server2 = appSettings["Server2"];
if (selfClear == true && !server1.Contains("localhost"))
{
string Server1IPAddress = server1.Split(':')[1].Substring(2);
if (UserModelSession.ServerIPAddress != Server1IPAddress)
{
server2 = server1;
}
NetTcpBinding tcpb;
string PropagationEndPoint = "";
PropagationEndPoint = server2 + "/CommonComponent";
tcpb = new NetTcpBinding();
tcpb.Security.Mode = SecurityMode.None;
EndpointAddress ep = null;
ep = new EndpointAddress(PropagationEndPoint);
using (ChannelFactory<ICommonComponent> factory = new ChannelFactory<ICommonComponent>(tcpb))
{
ICommonComponent proxy = factory.CreateChannel(ep);
using (OperationContextScope scope = new OperationContextScope((IContextChannel)proxy))
{
OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("usermodel", "utp_ns", ""));
proxy.ClearAllSysPreference(dealerSystemID, false);
}
}
}
return systemPreferenceModel;
}
catch (Exception ex)
{
bool rethrow = BusinessLogicExceptionHandler.HandleExcetion(ref ex);
if (rethrow)
{
throw ex;
}
return null;
}
}
在本地工作正常。查看如下
<div id="dvSysPref">
<div style="padding-bottom:0px; padding-top:5px;">
@(Html.Kendo().Grid(Model.mtSystemPreferenceViewModel)
.Name("SystemPreferenceGrid")
.Selectable()
.Columns(columns =>
{
columns.Bound(model => model.entityType).Width("50px").Title("EntityType");
columns.Bound(model => model.entityId).Width("150px").Title("EntityID");
columns.Bound(model => model.dealerSystemId).Width("150px").Title("Dealer SystemID");
columns.Bound(model => model.name).Width("150px").Title("name");
columns.Bound(model => model.valInt).Width("50px").Title("ValInt");
columns.Bound(model => model.valString).Width("150px").Title("ValString");
columns.Bound(model => model.createdDate).Width("150px").ClientTemplate("#= kendo.toString(createdDate, \"MM/dd/yy hh:mm tt\") #").Title("createdDate");
columns.Bound(model => model.lastUpdatedDate).Width("150px").ClientTemplate("#= kendo.toString(lastUpdatedDate, \"MM/dd/yy hh:mm tt\") #").Title("LastUpdatedDate");
columns.Bound(model => model.isActive).Width("150px").Title("IsActive");
columns.Bound(model => model.valBin).Width("100px").Title("ValBin");
})
.HtmlAttributes(new { style = "font-size: 14px; font-family: 'Segoe UI'; min-height: 100px; table-layout: fixed;" })
.Pageable(pg =>
pg.Refresh(true)
.PageSizes(new int[] { 10, 30, 50, 100, 200 })
.Enabled(true)
.PreviousNext(true)
.Numeric(false)
.Input(true)
.Messages(m => m.Page("Page ")
.Of(" of {0}")
)
)
.Navigatable()
.Filterable()
.Scrollable(scr => scr.Height(400))
.Sortable(sort => sort.SortMode(GridSortMode.SingleColumn))
.Resizable(resize => resize.Columns(true))
.Reorderable(reorder => reorder.Columns(true))
.NoRecords("No Records Found")
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.ServerOperation(false)
.Model(model =>
{
model.Id(p => p.mtSystemPreferenceId);
model.Field(p => p.entityType);
model.Field(p => p.entityId);
model.Field(p => p.name);
model.Field(p => p.valInt);
model.Field(p => p.valString);
model.Field(p => p.createdDate);
model.Field(p => p.createdBy);
model.Field(p => p.lastUpdatedDate);
model.Field(p => p.lastUpdatedBy);
model.Field(p => p.dealerSystemId);
model.Field(p => p.isActive);
model.Field(p => p.valBin);
}
)
.Read(read => read.Action("ReadSystemPreferenceListGrid", "ClearCache"))
)
)
</div>
<div style="padding-bottom:0px; padding-top:5px;">
@(Html.Kendo().DropDownList()
.Name("DealerSystemSysPreferenceList")
.BindTo(Model.SystemPreferenceDealerSystemViewModelList)
.DataTextField("DealerSystemID")
.DataValueField("ID")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("LoadSystemSysPreferenceListDropdown", "ClearCache");
})
.ServerFiltering(true);
})
.Events(e =>
{
e.DataBound("onSystemPreferenceDataBound");
})
.OptionLabel("Select Dealer System ID")
.HtmlAttributes(new { style = "width: 30%" })
)
<input type="button" id="btnSysPreferenceClearCache" class="btn btn-default btn-primary" title="Search" value="Clear Cache" />
<div>
<span id="dvSystemPreferenceListvalidMesg" class="k-widget k-tooltip k-tooltip-validation k-invalid-msg field-validation-error" role="alert" style="display:none;text-align:left;position:absolute;margin:0px!important;padding:1px!important;bottom: 400px;"><span class="k-icon k-warning"> </span> Please select a dealer system ID</span>
</div>
</div>
JQuery ajax代码,
function onSystemPreferenceDataBound(e) {
$("#btnSysPreferenceClearCache").click(function () {
var dealerSystemID = $("#DealerSystemSysPreferenceList").data("kendoDropDownList").text();
if (dealerSystemID != "Select Dealer System ID") {
$.ajax({
type: "GET",
url: '/ClearCache/ClearSystemPreferenceListCache',
data: { dealerSystemID: dealerSystemID },
pageSize: 20,
cache: false,
pageable: true,
success: function (src) {
alert('Systempreference Cache has been cleared for dealerSystemId '+dealerSystemID);
var dealerDropdown = [];
var dropdownList = [];
if (src.Data.length > 0) {
for (var i = 0; i < src.Data.length; i++) {
dropdownList.push({ text: src.Data[i].dealerSystemId, value: i });
}
//Removes duplicates from dropdown
dealerDropdown = dropdownList.reduce(function (item, e1) {
var matches = item.filter(function (e2)
{ return e1.text == e2.text });
if (matches.length == 0) {
item.push(e1);
}
return item;
}, []);
dealerDropdown.unshift({ text: "Select Dealer System ID", value: "0" });
}
else {
dealerDropdown = [
{ text: "Select Dealer System ID", value: "0" },
];
}
var dataSource = new kendo.data.DataSource({
data: src,
pageSize: 20,
schema: {
type: "json",
data: "Data",
total: "Total",
model: {
fields: {
mtSystemPreferenceId: { type: "number" },
entityType: { type: "number" },
name: { type: "string" },
dealerSystemId: { type: "number" },
valInt: { type: "number" },
valString: { type: "string" },
createdDate: { type: "date" },
createdBy: { type: "number" },
lastUpdatedDate: { type: "date" },
lastUpdatedBy: { type: "number" },
isActive: { type: "bool" },
}
}
}
});
$('#SystemPreferenceGrid').data('kendoGrid').setDataSource(dataSource);
$("#DealerSystemSysPreferenceList").kendoDropDownList({
dataTextField: "text",
dataValueField: "value",
dataSource: dealerDropdown
});
}
});
}
else {
alert('Please select a DealerSystemID');
}
});
}
我的控制器代码如下,
public ActionResult ClearUsersListCache(string dealerSystemID)
{
SupportViewModel supportVM = new SupportViewModel();
UsersViewModel usersListModel = new UsersViewModel();
supportVM.userViewModelList = new List<UsersViewModel>();
List<UserModel> UserListInfoModel = new List<UserModel>();
List<DealerSystemViewModel> dealerSystemViewmodel = new List<DealerSystemViewModel>();
supportVM.UsersDealerSystemViewModelList = new List<DealerSystemViewModel>();
DTNAUserModel dtnaUserModel = (DTNAUserModel)System.Web.HttpContext.Current.Session["DTNAConnectUser"];
DTNAUserModel systemUserLoginModel = new DTNAUserModel();
try
{
using (BusinessLayerServiceFactory<ICommonComponent> commonComponentServiceFactory = new BusinessLayerServiceFactory<ICommonComponent>())
{
ICommonComponent commonComponent = commonComponentServiceFactory.Instantiate();
using (OperationContextScope ocs = new OperationContextScope((IContextChannel)commonComponent))
{
OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader(commonComponentServiceFactory.UserModelName, commonComponentServiceFactory.UserModelNameSpace, commonComponentServiceFactory.SerializedUserModel));
UserListInfoModel = commonComponent.ClearAllUsers(dealerSystemID);
}
}
if (UserListInfoModel != null)
{
foreach (var user in UserListInfoModel)
{
UsersViewModel userViewModel = new UsersViewModel();
{
{
userViewModel.Gid = user.Gid;
userViewModel.mtUserId = user.mtUserId;
userViewModel.BranchId = user.BranchId;
userViewModel.DeptId = user.DeptId;
userViewModel.UserId = user.UserId;
userViewModel.DmsUserId = user.DmsUserId;
userViewModel.DealerSystemId = user.DealerSystemId;
userViewModel.Description = user.Description;
userViewModel.Email = user.Email;
userViewModel.Role = user.Role;
userViewModel.isActive = user.isActive;
userViewModel.Password = user.Password;
userViewModel.RecActive = user.RecActive;
userViewModel.Facility = user.Facility;
}
supportVM.userViewModelList.Add(userViewModel);
}
}
for (int i = 0; i < UserListInfoModel.Count; i++)
{
supportVM.UsersDealerSystemViewModelList.Add(new DealerSystemViewModel { DealerSystemID = UserListInfoModel[i].DealerSystemId.ToString(), ID = Convert.ToInt64(i + 1) });
}
//This removes duplicates
supportVM.UsersDealerSystemViewModelList = supportVM.UsersDealerSystemViewModelList.GroupBy(i => i.DealerSystemID).Select(g => g.First()).ToList();
}
var result = new DataSourceResult()
{
Data = supportVM.userViewModelList.OrderByDescending(x => x.mtUserId), // Process data (paging and sorting applied)
Total = supportVM.userViewModelList.Count // Total number of records
};
var jsonResult = Json(result, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
}
catch (Exception ex)
{
bool rethrow = UserInterfaceExceptionHandler.HandleException(ref ex);
if (rethrow)
{
throw ex;
}
return null;
}
}
然而,当它部署时,我们能够看到一个javascript错误'无法读取未定义的属性长度'。此错误仅在不在本地的服务器中发生。当我调试时,我可以看到在Jquery成功函数控制器返回视图而不是导致此错误的数据。因此在数据返回成功函数是未定义的,因此'src.Data.length'的长度属性是未定义的。 SystemPreferenceGrid 请注意我附加了一个屏幕截图,因为我们有一个网格会发生同样的错误。 但是我们无法在本地重现这个问题。那么有人可以帮我解决这个问题吗?