ByVal datax As ObjectI'm trying to use ASP.NET with webmethod to retrieve data and save data for jqgrid.
I can retrieve data from the webmethod but no luck to do the save.
The server side seems never get my post data. Would someone please help?
grid code:
$('#list99').jqGrid({
datatype: function(postdata) {
$.ajax({
url: 'dbtest.aspx/getdata',
editurl: 'dbtest.aspx/updatedb',
type: 'POST',
data: '{}',
dataType: "json",
contentType: "application/json; charset=utf-8",
error: function(data, textStatus) {
alert('Error loading json');
},
success: function(data, st) {
if (st == 'success') {
var grid = $("#list99");
var gridData = JSON.parse(data.d);
grid.clearGridData();
for (var i = 0; i < gridData.length; i++) {
grid.addRowData(i + 1, gridData[i]);
}
}
$("#list99").jqGrid('navGrid', '#pager99', { add: true, edit: true, del: true });
}
});
},
type: 'POST',
editurl: 'dbtest.aspx/updatedb',
colNames: ['customerid', 'customername'],
colModel: [
{ name: 'customerid', index: 'customerid', width: 80, align: 'left', editable: true, edittype: 'text' },
{ name: 'customername', index: 'customername', width: 120, align: 'left', editable: true, edittype: 'text'}],
pager: $('#pager99'),
rowNum: 5,
rowList: [10],
sortname: 'customerid',
sortorder: 'desc',
viewrecords: true,
//width: 300
autowidth: true
});
server side code:
Public Class customer
Public customerid As String
Public customername As String
End Class
<System.Web.Services.WebMethod()> _
Public Shared Function getdata() As String
Dim c1 As New customer
Dim c2 As New customer
c1.customerid = "1"
c1.customername = "pete"
c2.customerid = "2"
c2.customername = "joah"
Dim lstcustomer As New List(Of customer)
lstcustomer.Add(c1)
lstcustomer.Add(c2)
Dim jsonserial As New JavaScriptSerializer
Dim result As String
result = jsonserial.Serialize(lstcustomer)
Return result
End Function
<System.Web.Services.WebMethod()> _
Public Shared Function updatedb(ByVal datax As Object) As String
//attempt to do save
End Function
The function updatedb just never being called after i clicked "sumbit" after add/edit/delete.
After that i checked with firebug and i got the error message:
"Invalid web service call, missing value for parameter: 'data'."
I've also tried to add the following:
jQuery.extend(jQuery.jgrid.edit, {
ajaxEditOptions: { contentType: "application/json" },
recreateForm: true,
serializeEditData: function(data) {
//alert('in2');
//alert(postData.customerid);
//alert(JSON.stringify(postData));
if (data.customerid == undefined) { data.customerid = null; }
var postData = { 'data': data };
//alert(postData.customerid);
return JSON.stringify(postData);
}
});
It still didnt work =(
I don't recommend you ever use datatype defined as function especially if you do just jQuery.ajax to communicate with the server. jqGrid provide many ways to customize data which will be send to the server (see serializeGridData callback and ajaxGridOptions option of jqGrid for example) and to change the server response before the server response will be processed by jqGrid (beforeProcessing callback for example). The current code use addRowData to fill the data. It' the slowest way to fill the grid which I know. Moreover you call navGrid on every loading of the data instead of calling it once directly after the grid will be created. All other calls will be just ignored. You can find many examples how to use web servicing together with jqGrid. Look at the answer for example
Your current code mix the options of jQuery.ajax with the options of jqGrid. You placed editurl option of jqGrid into the list of options of $.ajax.
One more important error is the usage of correct names of variables in updatedb method. You use currently datax As Object instead of usage customerid and customername as parameters.
Related
I work in durandal project and using breeze.
I retrive with the server (c#) object from sql into c# class, and it enter to a breeze-object which look exactly like the c# class.
when I define in the breeze object property with data-type "date-time", it doesn't convert the date-time of c# to date-time of breeze correctly.
I checked and finded the problem:
when it arrives to this point, it doesn't work well:
function __isDate(o) {
return __classof(o) === "date" && !isNaN(o.getTime());
}
in the watching-window I see that: __classof(o)=> returns "string"!!!
so I looked in your site and see that there is option to use breeze.DataType.parseDateFromServer.
but I dont want to use it in the javascript- logic, becouse after it enter the c# object into the breeze object, and doesn't convert it correctly, I lost the original date, and now this option can't help me.
so I want to use this option into the breeze-object.
here is my code:
addEmployeePersonalDetailsType(store);
function addEmployeePersonalDetailsType(store) {
store.addEntityType({
shortName: "EmployeePersonalDetailsDTO",
namespace: "eHarmonyServer.Entities",
isComplexType: true,
dataProperties: {
BirthDay: { dataType: DataType.DateTime, isPartOfKey: false , validators: [Validator.date()] },
EmmigrationDate: { dataType: DataType.DateTime, isPartOfKey: false },
JobCode: { dataType: DataType.Int32, isPartOfKey: false },
ChildCount: { dataType: DataType.Int32, isPartOfKey: false, isNullable: false },
str_JobCode: { dataType: DataType.String, isPartOfKey: false },
str_MaritalStatus: { dataType: DataType.String, isPartOfKey: false },
validators: [Validator.double()]
},
});
store.registerEntityTypeCtor("EmployeePersonalDetailsDTO", null, null);
}
I want to write:
EmmigrationDate: { dataType: DataType.parseDateFromServer, isPartOfKey: false },
but parseDateFromServer is function and it needs to acceps one parameter called "source". what should I send to it?
or- do you have another way to solve this problem?
thank you very match.
sample of class in file model.js:
addEmployeeMainDataType(store);
function addEmployeeMainDataType(store) {
store.addEntityType({
shortName: "EmployeeMainDTO",
namespace: "eHarmonyServer.Entities",
isComplexType: true,
dataProperties: {
FirstName: { dataType: DataType.String, isNullable: true, isPartOfKey: false, validators: [Validator.required()] },
NickName: { dataType: DataType.String, isNullable: true, isPartOfKey: false },
LastName: { dataType: DataType.String, isNullable: true, isPartOfKey: false, validators: [Validator.required()] },
PassportNo: { dataType: DataType.String, isNullable: true, isPartOfKey: false },
Date_start: { dataType: DataType.DateTime, isNullable: true, isPartOfKey: false, validators: [Validator.required()] },
Date_end: {
dataType: DataType.DateTime, isNullable: true, isPartOfKey: false, validators: [Validator.date()
, validationHelper.complexTypeEndDateGreaterThanStartDateValidator({
startDateFieldName: "Date_start", resourceId: "1732",
entityName: 'employeeTblData',
entityNameSub: 'employeeMainData'
})]
},
HourPrice: { dataType: DataType.Double, isNullable: true, isPartOfKey: false },
xActive: { dataType: DataType.Boolean, isNullable: false, isPartOfKey: false },
BalanceCalc: { dataType: DataType.Boolean, isNullable: false, isPartOfKey: false }
}
});
store.registerEntityTypeCtor("EmployeeMainDTO", null, employeeMainDataInit);
}
as you can see, I have more than one property from type "dateTime": Date_start, Date_end.
and I have a few classes else, which have properties of dateTime type, too.
so, the solution that suggest me to write initializer function for each field which is type date- is not good for me.
I want that instead of write :
Date_start: { dataType: DataType.DateTime, isNullable: true,
Date_end: { dataType: DataType.DateTime, isNullable: true,
you will give me another type to write, that I will need to add only one converter function to code, and it will be generic function, not relative to specific field-name, such "birthday", "Data_end", etc.
One of durandal scripts changes the function "Date.parse" .
The new function cannot parse UTC date.
Here is my solution :
Add the following code to your model.js :
breeze.DataType.parseDateFromServer = function (source)
{
var _localTimeRegex = /.\d{3}$/;
if (typeof source === 'string') {
// convert to UTC string if no time zone specifier.
var isLocalTime = _localTimeRegex.test(source);
source = isLocalTime ? source + 'Z' : source;
}
source = new Date(source);
return source;
}
This will override the function parseDateFromServer (in breeze.debug.js) to not use Date.parse .
in your model.js (or however you call it where you define model on client side) you do following on initialization:
//...
metadataStore.registerEntityTypeCtor('MyClass', null, myClassInitializer);
//...
function myClassInitializer(myClassInstance){
myClassInstance.formattedDate = ko.computed({
read: function () {
return moment(myClassInstance.date()).format('YYYY-MM-DD');
},
write: function (value) {
myClassInstance.date(moment(value).toDate());
}
});
}
and then you just use formattedDate instead of date. Obviously, you need to translate into your variable names. Two-way binding from knockout ensures your date and formattedDate are always synced. Hopefully you get the point.
The other answer assumes that you are using moment.js for formatting your dates. Moment is an excellent library and you should definitely use it, but be wary of putting view formatting logic in your model definition or initialization. The correct way to handle what you are trying to do is with a custom binding handler that handles your presentation logic to display the date the way that you want to. To use this custom binding handler you do need to have moment.js referenced in your project.
Register a custom binding handler to handle date formatting in your view -
ko.bindingHandlers.Date = {
update: function (element, valueAccessor) {
var value = valueAccessor();
var date = moment(value());
var strDate = date.format('YYYY/MM/DD');
$(element).text(strDate);
}
};
And then you can pass a string, a date, or anything else in and get a formatted date (just make sure you use some sort of date standard) -
<span data-bind="Date: myDate"></span>
Keep in mind
This is to take in a date without having to change what the server is returning. If you can do that and return a UTC date or some other standard date format instead of a string this makes it even easier for you.
I am converting my application to use Server Side Paging with the Kendo Grid UI. Prior to switching serverPaging to true, I was properly displaying my grid contents, and paging on the client side. However, once I turned on the serverPaging, my data was no longer visible. I have checked the network call, and my data is returning (only 2 records of 8 total) as expected, but I am not seeing it in the grid.
Here is the grid construction:
$v.KendoGrid.makeGrid(gridName, {
columns: [
{ field: 'IdentifierCode', title: 'User Name' },
{ field: 'CompanyName', title: 'Company' },
{ field: 'Email', title: 'Email' }
],
dataSource: {
pageSize: 2,
schema: {
data: 'Data', // records are returned in the data section of the response
model: {
id: 'Id',
fields: {
IdentifierCode: { type: 'string' },
CompanyName: { type: 'string' },
Email: { type: 'string' }
}
},
total: 'Total' // total number of records are in the total section of the response
},
serverPaging: true,
transport: {
read: {
url: window.urlConfigs.root + "Security/UserAccount/PagedListing"
dataType: "json",
type: "GET"
}
}
},
editable: false,
filterable: true,
height: 464,
pageable: true,
scrollable: true,
sortable: true
});
Here is the MVC Controller method:
public ActionResult PagedListing(int pageSize, int skip)
{
var entities = ReadRepo.All();
var total = entities.Count();
var data = entities.Skip(skip).Take(pageSize).Select(MapEntityToViewModel).ToList();
return Json(new { Total = total, Data = data }, JsonRequestBehavior.AllowGet);
}
And here is the data I get back on the network call:
{"Total":8,"Data":[{"Id":"928f0bb2-608b-417b-bf6e-e5c58f85fec2","IdentifierCode":"admin","FirstName":"Administrator","MiddleName":"of","MiddleNameHuman":"of","LastName":"GasStream","DisplayName":"Administrator of GasStream","Email":"admin#example.com","IsExternal":false,"UserTypeHuman":"Internal","CompanyId":"75bb05a4-1ec2-4042-aeba-a229008aca9f","CompanyName":"Entessa Pipeline & Terminal, MLP","CompanyIdentifierCode":"SHA","Password":"wFg/a/NEU6WM8z4YZBUduitIDROfeFz/+Za6leAHnBE=","PasswordChanged":false,"ForceNewPasswordFlag":false,"Settings":[],"RoleGroups":[]},{"Id":"47c29025-cfa8-4447-9ab7-a229008ad088","IdentifierCode":"contractcarl","FirstName":"Carl","MiddleName":null,"MiddleNameHuman":"","LastName":"Smithers","DisplayName":"Carl Smithers","Email":"carl#entessa.com","IsExternal":false,"UserTypeHuman":"Internal","CompanyId":"75bb05a4-1ec2-4042-aeba-a229008aca9f","CompanyName":"Entessa Pipeline & Terminal, MLP","CompanyIdentifierCode":"SHA","Password":"IWdH+qDIOucNrre6V4AgI6Exm2Vq5qkIdXdsWfP6jn4=","PasswordChanged":false,"ForceNewPasswordFlag":false,"Settings":[],"RoleGroups":[]}]}
I suspect I have missed something small, but after looking at this and trying all sorts of possible work-arounds, I cannot see it, so I am asking for some help. I thought once I got the data to return small sets from the Server, things would get simpler.
Thanks in advance,
Drew
I ended up finding the answer. the $v.KendoGrid was a method that wrapped the kendoGrid call itself, and in there something was getting reset to not allow the data to be parsed properly when it came back from the server properly paged.
I have since re-worked the mess so I can establish the necessary parameters in the $v.KendoGrid call for just my type of grid.
Thanks for the help, and the eyes to catch the comma, Brett.
Drew
I want to return some JSON data from my WebAPI controller and I want the returned data to be like this.
{"rows":[{"id":1,"cell":["1","amila","amila","False"]},{"id":2,"cell":["2","rakhitha","rakhitha","False"]},{"id":3,"cell":["3","Chathura","Chathura","False"]},{"id":4,"cell":["4","Geethaga","Geethaga","False"]}]}
But when I use the below code,
return new System.Web.Mvc.JsonResult()
{
Data = jsonData,
JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet
};
the data is returned like this.
{"Data":{"rows":[{"id":1,"cell":["1","amila","amila","False"]},{"id":2,"cell":["2","rakhitha","rakhitha","False"]},{"id":3,"cell":["3","Chathura","Chathura","False"]},{"id":4,"cell":["4","Geethaga","Geethaga","False"]}]},"JsonRequestBehavior":0}
There is an additional JSON key as "Data". I dont want that parameter and as my implementation I cant remove this "Data" part after getting it to the client side. Because the data received from the server is directly used to fill a jqGrid. The code is below.
$("#Grid1").jqGrid({
url: 'api/matchingservicewebapi/GetUser',
datatype: 'json',
mtype: 'GET',
colNames: ['', 'Name', 'FullName', 'IsActive'],
colModel: [
{ name: 'Id', index: 'Id', width: 200 },
{ name: 'Name', index: 'Name', width: 300 },
{ name: 'FullName', index: 'FullName', width: 300 },
{ name: 'IsActive', index: 'IsActive', width: 300 }
],
rowNum: 10,
rowList: [10, 20, 30],
pager: '#pager',
sortname: 'Id',
viewrecoreds: true,
sortorder: "desc",
imgpath: 'Themes/images'
}).navGrid(pager, { edit: true, add: true, del: true, refresh: true, search: true });
How do I remove this "Data" part? Because when you have this "Data" key in the returned JSON, jqGrid is not capable of filling that data to the grid.
I am using WebAPI Controller to return this data. But I tried using MVC3 controller, then this "Data" key was not in the returned JSON and data filled to the grid successfully. But I want to use WebAPI Controller. Please help to solve this.
Thank you in advance.
JsonResult is an MVC concept. For Web API, your controller can simply return a CLR object and it will be serialized into JSON (assuming the client asks for JSON).
The result you're seeing is because the entire JsonResult object is getting serialized into JSON.
To get started with Web API, see: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
(Or you could continue to use an MVC controller ... is there a particular reason that you wanted to use a Web API controller?)
Json.NET works well for me. Just return the JObject in the Web API
http://james.newtonking.com/projects/json/help/html/CreatingLINQtoJSON.htm
JObject o = JObject.FromObject(new
{
channel = new
{
title = "James Newton-King",
link = "http://james.newtonking.com",
description = "James Newton-King's blog.",
item =
from p in posts
orderby p.Title
select new
{
title = p.Title,
description = p.Description,
link = p.Link,
category = p.Categories
}
}
})
set jsonreader option in jqGrid. like
jQuery("#gridid").jqGrid({
...
jsonReader : {root:"Data"},
...
});
http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_string
Use data.d instead of data in your code.
success: function(data){
//create jquery object from the response html
var response=$(data.d);
//Your binding logic here
}
You can return the data in jqgrid required format. If you don't want to create a separate class for this, use dynamic return type in apicontroller. See working example here.
jqGrid doesn't send postdata to my controller. I tried all provided solutions on stackoverflow, buy apparently I'm making error(s) somewhere. So here is the code:
function refreshGrid() {
alert('CompanyNamePart=' + $("#CompanyNamePart").val()); // to check if this is correct value
$("#list").trigger('reloadGrid');
return false;
}
$(function () {
var grid = jQuery("#list").jqGrid({
datatype: 'json',
caption: 'Transaction Log',
postdata: {
CompanyNamePart: function () { return $("#CompanyNamePart").val(); },
UsernamePart: function () { return $("#UsernamePart").val(); },
},
url: '#Url.Action("GetTransactionLogData")',
mtype: 'GET',
colNames: ['Ref.', 'TradeDate', 'Status'],
colModel: [
{ name: 'Reference', index: 'Reference', width: '60' },
{ name: 'TradeDate', index: 'TradeDate', width: '70' },
{ name: 'Status', index: 'Status', width: '80' }
],
pager: $('#pager'),
rowNum: 10,
height: '100%'
});
});
On Controller side I have simple code:
[HttpGet]
public ActionResult GetTransactionLogData(string sidx, string sord, int page, int rows, string CompanyNamePart, string UsernamePart)
{ return Json(null); }
and in Debugging mode when I call refreshGrid() by clicking a button I get one alert to confirm me that it reads correctly value of textfield, and after that it refreshes the grid. I receive call of controller, but values for CompanyNamePart and UsernamePart variables are all null, even though I filled them.
I tried another solution for postdata section with another approach, first I created functions that return needed values and put them in postdata section of grid:
function getCompanyNamePartVal() {
return $("#CompanyNamePart").val();
}
function getUsernamePartVal() {
return $("#UsernamePart").val();
}
... in jqgrid definition
postdata: {
CompanyNamePart: getCompanyNamePartVal(),
UsernamePart: getUsernamePartVal(),
},
but with no success.
When I checked Firebug, I could see that jqGrid is not sending postdata values:
Get Parameters caught by Firebug
_ 1340696638960
_search false
nd 1340696638955
page 1
rows 10
sidx
sord asc
What am I doing wrong?
The parameter is called postData, not postdata. Don't forget that javascript is a case sensitive language. So try like this:
postData: {
CompanyNamePart: function () { return $("#CompanyNamePart").val(); },
UsernamePart: function () { return $("#UsernamePart").val(); }
}
Also notice that I removed a trailing comma after the UsernamePart function that you had in your code and which produces invalid javascript. Some more sensitive browsers might not accept it.
$("#grid").jqGrid({
url: "/DocumentoBalanza/GetBalanzaEmpresaMes",
//postData: JSON.stringify(formDataParam),
postData : {
id: function () { return formDataParam.id; },
strDate: function () { return formDataParam.strDate; },
},
datatype: 'json',
mtype: 'GET',
public JsonResult GetBalanzaEmpresaMes(string sidx, string sord, int page, int rows, int id, string strDate)
Code OK.
I have implemented a simple jqGrid in my ASP.NET MVC 3 application. It shows the data correctly, so that's fine. But now I want my application to show the details of a row if I doubleclick on a row.
I have a Detail action method that actually gets called with the correct ID, the details are retrieved from database and the Details view is returned, so that seems to be OK, but in my application nothing happens.
I have the following script for the grid:
jQuery(document).ready(function ()
{
jQuery("#list").jqGrid({
url: '/Incident/ListData/',
datatype: 'json',
mtype: 'GET',
colNames: ['TicketNumber', 'Title', 'CreatedOn'],
colModel: [
{ name: 'TicketNumber', index: 'TicketNumber', width: 75, align: 'left' },
{ name: 'Title', index: 'Title', width: 250, align: 'left' },
{ name: 'CreatedOn', index: 'CreatedOn', width: 90, align: 'left'}],
pager: jQuery('#pager'),
rowNum: 10,
rowList: [10, 50, 100],
sortname: 'CreatedOn',
sortorder: "desc",
viewrecords: true,
width: 650,
imgpath: '/Content/themes/base/images',
caption: 'Incidents',
ondblClickRow: function (id) { $.get('/Incident/Detail/' + id); }
});
});
I've tried using $.ajax instead of $.get, but in both cases the details method gets called and nothing happens.
This is the Details action method
public ViewResult Detail(Guid id)
{
var query = from inc in _repository.Incidents
where inc.Id == id
select
new IncidentModel(inc)
{
CreatedOn = inc.CreatedOn,
Description = inc.Description,
ModifiedOn = inc.ModifiedOn,
TicketNumber = inc.TicketNumber,
Title = inc.Title,
Status = inc.Status
};
var incident = query.FirstOrDefault();
return View(incident);
}
$.get sends an AJAX request and gives you the server's reply.
It doesn't actually do anything with the server's reply; it's up to you to do something useful.
It sounds like you don't want AJAX at all; instead, you want to navigate to that page:
location = '/Incident/Detail/' + id
As Slaks said, you're not doing anything with the content. Should your double click event actually be redirecting the browser to that action?
(Just a side note, why double click? Nearly everything else net based uses a single click to browse).