Send additional data to Telerik MVC grid - asp.net-mvc-3

I am using AJAX bound Telerik MVC grid. I am sending data for the grid using a code like the following:
return View(new GridModel(...));
I want to be able to send additional data to client in the same action method. For example I need to send a single int value Total, which is to be shown above the grid (this value must be fetched from db, it cannot be evaluated client-side).
Please share any ideas.

To achieve your desired result, you could add a client footer template bound to the property passed down from your model as follows:
columns.Bound(o => o.UnitPrice)
.ClientFooterTemplate("<#= Total #>");
There is also a good example detailing the use of server aggregates on the Telerik site if you wanted to work these out using aggregate functions in the future.

Related

Is it normal that inputs (generated by html helpers) are influenced by POST data?

In a MVC app we implemented a mini forum. New posts in this forum are ajaxed. The AJAX POST action either returns a response form (partial view) or new post html and form html as a JSON.
New post and form are both rendered from views by this method. The model provided for the form has some null values, but the corresponding inputs store values taken from POST data (I verified the generated data to make sure it's not something that is done by the browser). The inputs are generated by html helpers (such as TextBoxFor).
So my question is, is this normal behavior in MVC and if it is, then how do I go about making those inputs have empty/null values (or even some specific value)? When debugging the values in the model are exactly as I set them (which is null, but same thing happens for any value really), but inputs for this very model still hold values taken from POST data.
I tested how does this work with good old PartialView instead of rendering html to string and returning it through JSON, but the results were exactly the same (so the method I use for rendering those views should be unrelated to the problem).
I came across this question: View data dictionary overriding model data in ASP.NET MVC
But from what I checked in my app, the POST data isn't actually stored in ViewData and the OP wasn't AJAXing data so redirects made more sense in his case.
I came across this post which explain this problem in detail and shows ways to deal with it.
To sum it up. Yes, this is normal behavior of htmlHelpers during POST action. To prevent it you can run ModelState.Clear(); in your post action (preferably just before you return\render the view). Optionally it is also possible to remove just one field using ModelState.Remove("PropName"); where PropName is the name of the model property which you don't want to be passed from POST data.

$fn.yiiGridView.update calls for multiple grids

In a view, I display two grids with data.
The user is able to submit data in the following way:
The data is sent using a form displayed in the same page
On submit, the form data is sent through an ajax call to a controller
The ajax call has a callback on success that executes
$.fn.yiiGridView.update("grid1");
$.fn.yiiGridView.update("grid2");
as both grids will have their data changed by the submitted form.
The $.fn.yiiGridView.update method will update the grids by retrieving the same page and extracting what is required.
In this particular situation, it will happen twice, which raises the need to find a way to avoid this extra call and reduce unnecessary traffic.
Is there a way to modify the yiiGridView callbacks to update both grids with a single call?
I've tried to hack the methods, but with no success.
Ok, after some struggle I have found a solution.
I realise that there's a field called ajaxUpdate that in the yiiGridView documentation (jquery.gridview.js line: 39) says :
ajaxUpdate: array, IDs of the containers whose content may be updated by ajax response
This value is extracted from the CGridView parameters (http://www.yiiframework.com/doc/api/1.1/CGridView#ajaxUpdate-detail)
So I defined
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'grid1', //or could be grid2
'dataProvider' => $model->search(),
'ajaxUpdate'=>'grid1,grid2', //string separated with commas
......
));
and now just by calling
$.fn.yiiGridView.update("grid1"); //or grid2 if you set ajaxUpdate in grid2
both grids will get updated.
I wonder if there's a way to override the value using the options parameter for the gridview function (using the $('#grid1').yiiGridView('update',options) syntax)

Kendo UI Datasource and Arrays

I am sending over a series of array values from a posted form to an MVC3 Controller. I was hoping the default modelbinder would be able to parse this but I'm having some difficulty with it.
The array is in the following format:
order[0].[type]=some value.
I think this is the reason the model binder is not parsing my values because I'm not getting anything populated in my model.
What would be another way to handle this?
Probably need to post more of your code so I can see what you are doing exactly. However saying this you need to pass the model to the view/partial view on the response you are trying to retrieve on the post request.
If not you will have to iterate through the Form Collection that will be returned and the Actions Methods type e.g. ActionMethodName(FormCollection form), one issue is name versus id its the name of the Kendo UI control that is used to get the value not the id.
1As far as I remember the right format was:
orders[0].OrderID=13;
orders[0].Name="test";
orders[1].OrderID=15;
orders[1].Name="again test";
The indexing should start from 0 and increase by 1.
Check this out: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

What is the best way to do this in ASP.NET MVC3?

I want to have a form which has a simple 2 step process:
(If a person on has one school assigned to them then skip straight to step 2)
Step 1 - Select a school from a drop-down list
Step 2 - Enter the required data from a data-entry form. Certain fields are disabled based on which school was selected.
I have had a look at the various methods for creating 'wizards' and at using partial Views. What is the best way to handle this? I was wondering if using AJAX is worthwhile considering or just having a two step process in the form.
James :-)
You should absolutely do this via ajax so your options are either
use small partial views. your view logic determines what to disable/enable
(probably easier and more lightweight) use json to get a list of property names to disable.Then you can simply disable them via jQuery ideally by iterating through each item with the .each() call.
See: looping through JSON array in a jQuery list
for a use of the each call.
got jQuery getJson, see: http://api.jquery.com/jQuery.getJSON/
so:
1. getJson to get the results from a controller
2. enumerate using .each() and set the property
$("#" + yourFieldName).attr("disabled","disabled");

Validate data from CakePHP form with jQuery (AJAX)

I would like to validate both single field and multiple field data from a CakePHP form.
The single field validation should be done on blur from each field while the multiple field validation should be done on submitting the form.
I would like to use the $validate property declared in the Model for validating data and I would like to display the errors near each field (single field validation) and on top of the form (for multiple field validation).
My main goal is to achieve this the most "caky" way (if there is one for validating data with jQuery). I couldn't find any useful advice out there and I'm asking you for some help to get this going.
One of my concerns is how shall I pass data from the form to jQuery and then to the action that does the validation and also how shall I return and display the errors, if there are any.
Thank you in advance!
I'd suggest first making sure everything works without jQuery, then use the jQuery Form plugin to submit your forms via AJAX. If you include the RequestHandler component in your AppController, you should find that your controllers distinguish automatically between AJAX and synchronous requests.
OK, so I coded my own solution to this, but I am still waiting for a more "caky" approach.
I made two generic jQuery functions, one for single field validation and one for multiple field validation. The function should grab the data from the specified form and send it to the form's action via AJAX, to a specially created controller method which will attempt to validate data and output an AJAX response ("" for validation has passed and errors for errors in validation). Then, the result is checked in the jQuery function and the default form behaviour is triggered only if the validation has passed. Otherwise, display the errors and return false; to prevent default submission.

Resources