Reputation: 374
I'd like to dynamically generate some JqGrid. To do so I loop over a list in order to create the HTML content.
@foreach(string clientCode in ViewBag.Codes)
{
<table class="jqgCode" id="jqgCode+@clientCode" cellpadding="0" cellspacing="0"></table>
<div id="pjqgCode+@clientCode" style="text-align:center;"></div>
}
I dynamically generate the id of the grid with the value of the code which is unique.
Basically the grid will have the same format only the data will change, and the code is the parameter that will change the SQL Request. So what I want to do is to retrieve the grid id in order to give to my action in my Controller.
Here is what I'm trying to do:
<script type="text/javascript">
$(document).ready(function () {
$('.jqgCode').jqGrid({
//url from wich data should be requested
url: '@Url.Action("CodeDetail")',
//type of data
datatype: 'json',
//url access method type
mtype: 'POST',
postData: {code: $(this).attr('id')},
.........
});
});
When I'm looking in the action method "Code Detail" in debug mode, I see that I have a null value in the "code" parameter.
How can I do to have the grid id in my controller. Maybe there is a better way to do what I want to do, so all advice is good to have.
Thanks in advance!
Upvotes: 0
Views: 1190
Reputation: 221997
One problem is that $('.jqgCode')
must return exactly one element ($('.jqgCode').length
must be 1). If you don't would do this you can have two grids using the same URL ('@Url.Action("CodeDetail")'
) and id duplicates in the data returned from the server.
In case if $('.jqgCode').length
is equal to 1 you can rewrite your JavaScript code as the the following:
$(document).ready(function () {
var $myGrid = $('.jqgCode'), gridId = $myGrid.attr('id');
$myGrid.jqGrid({
url: '@Url.Action("CodeDetail")',
datatype: 'json',
mtype: 'POST',
postData: {code: gridId},
.........
});
});
Upvotes: 1