C# - Object Unidentify When Using Jqgrid.jqGridExport() - How To Use It For Exporting The Grid In Json
Mar 3, 2011
This is the javascript code i tried to export my grid data in json. it throws an exception and i can't figure out why. when i downloaded the jqgrid i checked the import/export module. I want to insert the json in a hidden field in order to get the data on the server side for validating and saving.
I was doing the MusicStore Tutorial, I finished it and now I am trying to improve the tables with the jqgrid, that seems to be great. I found the
Phil Haack blog's post about how to use it.
[URKL]
But I couldn't do a mix of this, I tried to make the storemanager page with the grid of jqgrid instead of the simple table, but doesn't work. This is what I did:
I've created a simple (or what I thought was simple!) search form using Razor, MVC3 and the webgrid from the web helpers. I enabled paging, used a great article I found here to get that working... or so I thought. Basically what happens is that when I click the page buttons at the bottom of the grid it does something that I didn't expect, and creates a JSON object, then the browser prompts me to try and save it, and not as I expected to refill the grid. I can't see what I'm doing wrong, and before I break my monitor by throwing it onto the railway line!
[Code]....
My jQuery code
[Code]....
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MvcApplication1.Models; using System.Configuration; using System.Web.Helpers; namespace MvcApplication1.Controllers { public class SearchController : Controller { // // GET: /Search/ Entities t = new Entities(ConfigurationManager.ConnectionStrings["Entities"].ConnectionString); public ActionResult Details(int id) { Tender_Question q = (from x in t.Tender_Questions where x.Unique_ID_number == id select x).SingleOrDefault(); Customer c = (from cu in t.Customers where cu.CustomersID == q.Company select cu).SingleOrDefault(); ViewBag.Customers = c.CompanyName; Panel_type pan = (from pa in t.Panel_types where pa.ID == q.Panel_type select pa).SingleOrDefault(); ViewBag.Panel = pan.Panel_type1; return View(q); } [HttpGet] public ActionResult Edit(int id) { Tender_Question q = (from x in t.Tender_Questions where x.Unique_ID_number == id select x).SingleOrDefault(); List<Customer> c = (from cu in t.Customers select cu).ToList(); ViewBag.Customers = c; return View(q); } [HttpPost] public ActionResult Edit(Tender_Question model) { if (ModelState.IsValid) { Tender_Question q = (from x in t.Tender_Questions where x.Unique_ID_number == model.Unique_ID_number select x).SingleOrDefault(); q.Answer = model.Answer; q.Company = model.Company; q.Customer = model.Customer; q.Date = model.Date; q.Page_Question_Ref = model.Page_Question_Ref; q.Panel_type = model.Panel_type; q.Question = model.Question; q.Type_of_tender = model.Type_of_tender; //add update code t.SaveChanges(); return RedirectToAction("Index"); } else { return View(model); } } [HttpGet] public ActionResult Search(int? page) { if (page.HasValue) { return RedirectToAction("EfficientPaging", new { page = page }); } else { return View(); } } [HttpPost] public ActionResult Search(int? page, FormCollection collection) { TempData.Clear(); IEnumerable<Tender_Question> q = getSearch(collection); TempData.Add("Results", q); return View(q); } private IEnumerable<Tender_Question> getSearch(FormCollection collection) { string question; string answer; IEnumerable<Tender_Question> q = t.Tender_Questions; question = collection["Question"]; answer = collection["Answer"]; if (!string.IsNullOrEmpty(question)) { if (question.EndsWith(",")) { question = question.Substring(0, question.Length - 1); } if (question.Length > 0) { if (question.Contains(",")) { string[] questioned = question.Split(','); foreach (string s in questioned) { q = q.Where(m => m.Question.Contains(s)); } } else { q = q.Where(m => m.Question.Contains(question)); } } } if (!string.IsNullOrEmpty(answer)) { if (answer.EndsWith(",")) { answer = answer.Substring(0, answer.Length - 1); } if (answer.Length > 0) { if (answer.Contains(",")) { string[] answered = answer.Split(','); foreach (string s in answered) { q = q.Where(m => m.Answer.Contains(s)); } } else { q = q.Where(m => m.Answer.Contains(answer)); } } } return q; } [HttpGet] public ActionResult EfficientPaging(int? page) { IEnumerable<Tender_Question> q = (IEnumerable<Tender_Question>)TempData["Results"]; int skip = page.HasValue ? page.Value - 1 : 0; q = q.OrderBy(o => o.Unique_ID_number).Skip(skip * 10).Take(10).ToList(); var grid = new WebGrid(q); var column = new WebGridColumn(); column.CanSort = false; column.ColumnName = "Unique_ID_number"; column.Header = "Edit"; column.Format = (item) => { return new HtmlString("<a href="/Home/Edit/" + column.ColumnName + "">Edit</a> "); }; var detcolumn = new WebGridColumn(); detcolumn.CanSort = false; detcolumn.ColumnName = "Unique_ID_number"; detcolumn.Header = "Details"; detcolumn.Format = (item) => { return new HtmlString("<a href="/Home/Details/" + column.ColumnName + "">Details</a> "); }; var htmlString = grid.GetHtml(htmlAttributes: new { id = "DataTable" }, columns: grid.Columns( column, detcolumn, grid.Column("Question"), grid.Column("Answer"), grid.Column("Company"), grid.Column(columnName: "Type_of_tender", header: "Type of Tender"), grid.Column(columnName: "Panel_type", header: "Panel type"), grid.Column(columnName: "Page_Question_Ref", header: "Page Question Ref"), grid.Column("Date") )); return Json(new { Data = htmlString.ToHtmlString(), Count =q.Count() / 10 }, JsonRequestBehavior.AllowGet); } } }
I have a project in ASP.NET MVC1 using VB.NET controlers and JqGrid. it works fine under MVC1. After migrating the project to ASP.NET MVC2, the grid is no longer populated. It seems that there is some new restrictions on returned Jsonresult in MVC2. How to solve this in VB.NET. Controler function populating the jqgrid is something like this :
Function GetGridRecordset(ByVal qry As String) As JsonResult Dim result = New JsonResult() ... ... Return result End Function
We are trying to fill JqGrid in ASP.NET MVC3 application where datatype is set to 'jsonstring' and in it's action controller, we set the json data string in ViewData and retrieve the same in javascript function where we used it to fill the Jqgrid.
// Action code
[Code]....
// Javascript code to fill Jqgrid [Code]....
[Code]....
There is an error while filling grid and when i alert the data it shows " in place of " in data, as below.
The decodeURI function do not work on strData and it always show " in place of " in the data. We do not want to replace the " with " in a loop as it slow the page with large data.
I use the JavaScriptSerializer class of ASP.net to serialize my object and return it to the client side. How can I deserialize the string using JavaScript?
I've scenario where I want to insert data into database without post back. there are around 12 to 13 fields which i need to insert. I'm passing DTO from the client side which is actually Json object. Now the problem which i'm facing is how to convert that Json object which i got in webservice to the "class" (in my case class name is User) object.
[Code]....
In the above case AddNewUser method takes the object of User class. But i'm getting casting error. So how do I convert Json object to the "User" class object?
Is there an easy way to populate my C# Object with the JSON object passed via AJAX?
//This is the JSON Object passed to C# WEBMETHOD from the page using JSON.stringify {"user":{"name":"asdf","teamname":"b","email":"c","players":["1","2"]}} //C# WebMetod That receives the JSON Object [WebMethod] public static void SaveTeam(Object user) { } //C# Class that represents the object structure of JSON Object passed in to the WebMethod public class User { public string name { get; set; } public string teamname { get; set; } public string email { get; set; } public Array players { get; set; } }
in my Asp.net web page (C# on backend) I use a Repeater, whose items consist of a title and a Flex chart (embedded .swf file). I am trying to export the contents of the Repeater to a Word document. My problem is to convert the SWF files into images and pass it on to the Word document.
The swf object has a public function which returns a byteArray representation of itself (public function grabScreen():ByteArray), but I do not know how to call it directly from c#. I have access to the mxml files, so I can make modifications to the swf files, if needed.
i want to do sorting for jQGrid in ASP.NET. For that i have pass the sidx and sord for column name and sorting order.but it was giving error "Incorrect syntax near '0'. Statement(s) could not be prepared." i wrote query for this select Image,CategoryName,Status,CategoryId from Category Order by {0} {1}
I am using Jqgrid 3.6 beta version with asp.net and i wanna add dynamic context menu on row selection.When ever user rightclick a row in jqgrid he wants to see some menu like file,edit and other menus. I have completed using single page i.e. both the jqgrid and its parent(JS files) are in single page . But in my main Application I have one master page in which ill define all the JS and seperate page for the content(Jqgrid). So what happens is the same code which is running in single page application is not working in the master detail page.
From VB.net code behind when I am trying to deserialize it it's stating that
"Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''."
Deserialization code snippet:
Dim serializer = New DataContractJsonSerializer(GetType(List(Of Employee))) Dim memoryStream = New MemoryStream() Dim s = msg.Content.ReadAsString() serializer.WriteObject(memoryStream, s) memoryStream.Position = 0.......
I want to serialize an object to json, so I'm using [Code]....
[Code]....
This is fine, but I want to return different json datasets depending on the circumstances. Lets say I have this object (a simplified example):
[DataContract] class foo { [DataMember] int productId [DataMember] string productName double price; //not used in json.. yet }
Which will serialize to a json object containing the product name & Id, but what if I also need to be able to return a different json object (say, productId and price but not name)? Can this be done somehow in the same object using WCF/DataContracts and DataMembers? This will be sent in an ajax query containing a collection of objects and needs to fast, so just including every member in every type of json request whether it's needed or not isn't a good solution.
I have an object called MyObject that has several properties. MyList is a list of MyObject that I populate with a linq query and then I serialize MyList into json. I end up with something like this
List<MyObject> MyList = new List<MyObject>();
MyList = TheLinqQuery(TheParam);
var TheJson = new System.Web.Script.Serialization.JavaScriptSerializer();
string MyJson = TheJson.Serialize(MyList);
What I want to do is serialize only parts of MyObject. For instance, I might have Property1, Property2...Propertyn and I want MyJson to only include Property3, Property5 and Property8. I thought of a way to do this by creating a new object with only the properties I want and from there create a new list for the serialization.
I could use the DataContractJsonSerializer Class and have custom Classes defined. However, am not sure how keys like "$" would get converted to in .Net.
I am unable to convert JSON string to .net object in asp.net. I am sending JSON string from client to server using hidden field (by keeping the JSON object.Tostring() in hidden field and reading the hidden field value in code behind file)
I have a json object collection of geo locations that I build in the server. Each of those objects has two properties: "marker" and "onClick". Marker is for storing a Google Maps marker object and the onClick stores the name of the function to be called when that marker is clicked on the map.When I'm pushing the location objects into an array using javascript in the client side, I create the markers and assign them to each location object within the array.
My problem is that when I bind the marker with the onClick property, the function won't be found in the DOM and get an error.Is there a way to declare a property in a json object for using it on an event binding?