?
Solved

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

Posted on 2011-10-25
7
Medium Priority
?
669 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:ttist25
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:vbigham
ID: 37025427
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
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 400 total points
ID: 37025434
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37025438
Ah. Me being bit slow.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:ttist25
ID: 37026336
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
 
LVL 1

Author Comment

by:ttist25
ID: 37026523
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
 
LVL 7

Accepted Solution

by:
vbigham earned 1600 total points
ID: 37026761
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
 
LVL 1

Author Closing Comment

by:ttist25
ID: 37027195
WOOOOOOOOOOOOOOOOOOOOOO HOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

:D

THANKS!!!!!!!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month16 days, 18 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question