Search code examples
javascriptc#asp.net-mvclinqpetapoco

Create select list in MVC application using peta poco ORM


In previous projects I have used LINQ to query a database table and then bind the results to a drop down list in an MVC application.

For example in my view:

$.getJSON('@Url.Action("ControllerAction")', function(data) {
   $(".class").empty();
   $.each(data, function (i, c) {
   $('.class').append('<option value="' + c.Value + '">' + c.Text + '</option>');
   });
$.ajaxSetup({ cache: false});      //If the SQL view changes, ensure that the user does not see old data.
});

And in my controller:

Dim ListItems As Generic.List(Of ClassName)

ListItems = (From x In c1 Select (New ClassName With {.Value = x.Value, .Text = x.Value})).ToList

I'm now learning how to develop a similar application using PetaPoco as the ORM. However I am struggling to bind the data from the database.

Is it possible to achieve this using PetaPoco?

I understand that I will need to first write the initial query

var dataContext = new PetaPoco.Database("sqlserverce");
var Something = dataContext.Query<Models.Something.ClassName>("Query");

Any comments would be a big help

Thanks James.


Solution

  • As it has been some time since I asked this question and as I have resolved my issue, I thought it was best to post this and provide an official answer for the question.

    Thank-you to ClearLogic for the help.

    Using .Fetch<> solved the issue and my final query ended up looking something like this

    var x = dataContext.Fetch<ClassNameToRepresentListItems>("DefiningQuery");