2
votes

My issue involved using Entity Framework / Kendo Grid and Web API 2 controllers and no ASP.NET MVC / Razor. All my code is either C# in controllers or straight up HTML / JavaScript.

It would appear that Kendo Grid are more geared for using tables / views directly rather than using stored procedures, am I correct in this assessment, or not?

I am trying to use one stored procedure in EF with a custom action name to return an IEnumerable and it populates the grid just fine, but when I try to hit the 'save changes' button, it attempts to do a POST and a PUT (I actually do think it's doing a POST because one of the rows does not have a primary key defined).

I should also mention that I think the model is not entirely the same, as the fields that get returned in the grid, don't necessarily correlate 1:1 with what gets fired in my save/update procedure. I have more fields being returned to me in the GET, and I have the model specified in the kendoDataSource, but can I customize and pick out what's being sent back when I click save changes?

During debugging, I see the correct lines in the network inspector show up, but when I look in the POST method that its hitting in the Web API 2 Controller I can't even see any values when I hover over the object being returned - which could be another issue.

So at the core, my questions are...

  1. Is it possible to have one model return values in a GET procedure to the kendo grid, and have an entirely different model get returned in the PUT/POST procedures on this grid, during batch save? It really seems that Kendo Grids prefer using tables directly, or views, but it's policy that I can't be touching tables at all, and we prefer not to use views, we prefer to go entirely stored procedures.

  2. Does the kendo grid, when doing a batch update/save, even though it says it's batch, is it ultimately sending data over per row, so one at a time? Or is it literally sending data over as one batch?

  3. We're using the grids to retrieve dynamic data, the rows will not be fixed, I've worked with templates a little to add CSS classes to the elements that wrap the cells, but that won't be flexible enough, but in saying that, I wonder if there's a way to yank the data out of the grid, and throw it into some hidden table or values on the front end and have a separate save button out of scope of the grid itself, to do a save of the user's newly typed in values...?

Thank you in advance

2
because one of the rows does not have a primary key defined - that's the very first problem that you should fix right away! Every table needs to have a proper primary key - no discussionmarc_s
Are you using a Auto-Increment field? If so, you might need to "re-read" on your dataset after insert or you could have dup keys on the client side.Ross Bush

2 Answers

0
votes

For updates it is very easy to use stored procedures. You will have a method like

public ActionResult Products_Update([DataSourceRequest]DataSourceRequest request, Product product)

And here you can fish out all properties and use them as parameters in a stored procedure.

For selecting then it would be hard to avoid tables. This is because all paging and sorting is based on an IQueryable that basically is a piece of sql. it is then given the paging and sorting in an extension method. You have a method like this:

public ActionResult Products_Read([DataSourceRequest]DataSourceRequest request)

And you can ofcourse fish everything out of the request and handle sorting and paging manually, but with a view or table you can just call the extension method and get everything:

var result = products.ToDataSourceResult(request);
0
votes

So, I figured out my issue.

1.) In the controller I needed to have a method declaration similar to this...

public IHttpActionResult UpdateProcedure([FromBody]IEnumerable<somevalue> models)

I think the key here was the [FromBody]IENumerable models

and

2.) In the Kendo Grid, I need to sure that...

I have this line in the DataSource (I put on the READ, UPDATE and CREATE calls

contentType: "application/json; charset=utf-8", 

and

batch: true

and

        parameterMap: function(options, operation) {
            if (operation !== "read" && options) {
                return JSON.stringify(options.models);
            }

        }

The combination of these things at least enabled me to see the model and values in the Web API 2 Controller.

Thanks for all the assistance.