How to get "Id" field to "DT_RowId" without massive recoding

How to get "Id" field to "DT_RowId" without massive recoding

psenechalpsenechal Posts: 32Questions: 2Answers: 0
edited December 2012 in Editor
All of my tables have an auto-incrementing column named Id. This is ideally what should be used for DT_RowId. I'm using .NET MVC and have found an INCREDIBLY simple way to get the data into dataTables...we're talking 2 lines of code simple. The problem is that the Id gets passed as "Id" instead of "DT_RowId", meaning that I can't delete records using the editor (update works fine because I just pass the Id back as a hidden form element).

It doesn't really make a whole lot of sense to completely re-code what I'm pulling from the database and put it into a new object that simply changes the name of "Id" to "DT_RowId". Is there a way I can map "Id" to "DT_RowId", or is there a way I can pass another value on the delete method? Right now, the only thing that gets passed is data[] which would normally be the RowId if I had it mapped.

Thanks for any ideas you might have. I suspect that there are a lot of people who don't name their columns DT_RowId and would love to be able to use queries directly from their database. Here's how I'm doing it if you're interested.

Controller
[code]
[HttpGet]
public JsonResult List() {
var reporttypes = _reporttypeService.Get().OrderBy(x => x.SortOrder);

return Json(new { aaData = reporttypes }, JsonRequestBehavior.AllowGet);
}
[/code]

Service
[code]
public IEnumerable Get() {
return from x in _reporttypeRepository.Table
select x;
}
[/code]

This gives me a properly formatted JSON object with everything I need except it has "Id" instead of "DT_RowId"

BTW: the forum software is screwing up the code display above...it's supposed to read aaData = reporttypes

Replies

  • psenechalpsenechal Posts: 32Questions: 2Answers: 0
    edited December 2012
    Well...I created a ViewModel that contains "DT_RowId" and "id" since dataTables didn't seem to like my "Id" with a capital letter. Right now I'm just mapping my Id column to id and DT_RowId in the controller on the List and Create actions. Everything seems to be working okay.

    Is there a client side solution that I can implement in the View? I'd like to keep the Controller as clean as possible. What I would need to do is take my JSON result, add "id" and "DT_RowId" to it, then copy the value of "Id" over to "id" and "DT_RowId". If there's any way of doing this, could you point me in the right direction?

    Much appreciated. If I can get this working properly and efficiently with our intranet site (it's a custom application built on top of Orchard CMS btw), I'm pretty sure I can get my boss to go for at least 1 developer license. He might even spring for a 2nd so my backup can develop with it as well.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    > Right now I'm just mapping my Id column to id and DT_RowId in the controller on the List and Create actions. Everything seems to be working okay.

    Currently this sounds like the right thing to do.

    The next release of Editor (1.2.3 - this particular feature is being worked on today in fact!) will include an `idSrc` option which will allow you to specify a JSON property from which to read the row's ID, rather than requiring the DT_RowId to be set.

    Regards,
    Allan
  • psenechalpsenechal Posts: 32Questions: 2Answers: 0
    That's awesome Allan! Will be looking forward to it. One more feature to incorporate into my test model before I present it to the big guy for purchase consideration.

    Thanks for the update!
This discussion has been closed.