Convert C# controller JSON action to VB.NET for MVC

Good morning,

I've been banging my head against the keyboard all night trying to figure this out so I thought I'd go make some coffee and give the experts a go at it.  

I'm trying to get a working version of jqGrid for jquery.  I'm using this tutorial from haacked.com and thus far I've been able to get a static version working using this in my controller:
 
Public Function GridData() As JsonResult
            Dim jsonData = New With { _
            Key .total = 1, _
            Key .page = 1, _
            Key .records = 3, _
            Key .rows = New Object() {New With { _
            Key .id = 1, _
            Key .cell = New Object() {"Brando", "Marlon", "J"} _
            }, New With { _
            Key .id = 2, _
            Key .cell = New Object() {"Deniro", "Robert", "K"} _
            }, New With { _
            Key .id = 3, _
            Key .cell = New Object() {"Pacino", "Al", "L"} _
            }} _
            }
            Return Json(jsonData, JsonRequestBehavior.AllowGet)

        End Function

Open in new window


This works fine but I need to get data from my table into the grid.  I'm using a Linq to SQL model (Grid.dbml) with my table (Persons_t).  The C# example is as follows:
 
public ActionResult LinqGridData
    (string sidx, string sord, int page, int rows) {
  varnew HaackOverflowDataContext();

  var jsonData = new {
    total = 1, //todo: calculate
    page = page,
    records = context.Questions.Count(),
    rows = (
      from question in context.Questions
      select new {
        id = question.Id,
        cell = new string[] { 
          question.Id.ToString(), question.Votes.ToString(), question.Title 
        }
      }).ToArray()
  };
  return Json(jsonData, JsonRequestBehavior.AllowGet);
}

Open in new window


In case it helps, here's a sample of the JSON format grid is looking for (according to the tutorial):
 
{ 
  total: "xxx", 
  page: "yyy", 
  records: "zzz",
  rows : [
    {id:"1", cell:["cell11", "cell12", "cell13"]},
    {id:"2", cell:["cell21", "cell22", "cell23"]},
      ...
  ]
}

Open in new window


So, my question is what would the VB.NET syntax be to make an equivalent Function LInqGridData for VB.NET (I'm required to work in VB.NET).  I've tried using translators on the web but they seem to choke on this.

Thanks in advance for any help you can provide.  It will be GREATLY appreciated!  :)

B

PS - I am not adverse to an XML solution rather than JSON if it would be more easily achieved.  The plugin accepts both.
LVL 1
ttist25Asked:
Who is Participating?
 
vbighamConnect With a Mentor Commented:
Sorry, the 'As' should have been '=', plus there were 2 missing commas:
Public Function LinqGridData(ByVal sidx As String, ByVal sord As String, ByVal page As Integer, ByVal rows As Integer) As ActionResult
        Dim context As New GridDataContext
        ' TODO: Calculate total '    
        Dim jsonData = New With { _
            .total = 1, _
            .page = page, _
            .records = context.Persons_t.Count(), _
            .rows = (From p In context.Persons_t _
                Select New With {.id = p.ID, .cell = {p.Name, p.Address, p.HomePhone, p.CellPhone}}).ToArray()}

        Return Json(jsonData, JsonRequestBehavior.AllowGet)
    End Function

Open in new window

0
 
vbighamCommented:
Does this help at all?
 
Public Function LinqGridData (ByVal sidx As String, ByVal sord As String, ByVal page As Integer, ByVal rows As Integer) As ActionResult
    Dim context As New GridDataContext

    ' TODO: Calculate total '
    dim jsonData As New With { _
        .total = 1, _ 
        .page = page, _ 
        .records = context.Persons_t.Count() _ 
        .rows = (From p In context.Persons_t _ 
                 Select New With {.id = p.ID .cell = {p.Name, p.Address, p.HomePhone, p.CellPhone}}).ToArray()

    return Json(jsonData, JsonRequestBehavior.AllowGet)
End Function

Open in new window

0
 
CodeCruiserConnect With a Mentor Commented:
Try this


Public Function LinqGridData (sidx As String, sord As String, page As Integer, rows As Integer) As ActionResult
Dim context As New HaackOverflowDataContext();
Dim jsonData = New With { _
	.total = 1, _
	.page = page, _
	.records = context.Questions.Count(), _
	.rows = ().ToArray() _
}
Return Json(jsonData, JsonRequestBehavior.AllowGet)
End Function

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CodeCruiserCommented:
Ah. Me being bit slow.
0
 
ttist25Author Commented:
Hey thanks for these.  I had to run out to a jobsite but I'm back now and I'm gonna give these a shot.  

Sorry for the delay responding.  
0
 
ttist25Author Commented:
Ok - I added this to my controller and it doesn't like:
dim jsonData As New With { _

Open in new window


The error is:
"Keyword does not name a type"

Also for this line:
Return Json(jsonData, JsonRequestBehavior.AllowGet)

Open in new window


I'm getting:
"jsonData is not declared.  It may be inaccessible dur to its protection level.

Thanks again for the help.
0
 
ttist25Author Commented:
WOOOOOOOOOOOOOOOOOOOOOO HOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

:D

THANKS!!!!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.