This is probably the dumbest question ever here on stack Overflow. But I am getting the weirdest results from some code that I am working with. I am trying to get jqGrid to work in my MVC 2 application.
My home controller has an action method for Index and GridData… GridData takes 4 parameters, 2 of them cannot be null so I add a defalutValue attribute with a value of one to them. The Index controller redirects to the GridData action method wich then opens up a GridData view… I don’t return the View in this function but I return a Json variable…
[Authorize(Roles="testRole")]
public ActionResult Index(string nextButton)
{
ViewData["identity_Name"] = identity.Name;
if (nextButton != null)
return RedirectToAction("GridData");
return View("Index");
}
public ViewResult windowsID()
{
return View();
}
public ActionResult GridData(string sidx, string sord, [DefaultValue(1)] int page, [DefaultValue(1)] int rows)
{
var jsonData = new
{
total = 1, // we'll implement later
page = page,
records = 3, // implement later
rows = new[]
{
new {id = 1, cell = new[] {"1", "-7", "Is this a good question?"}},
new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?"}},
new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?"}}
}
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
}
Here is most of my Javascript code.
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery("#list").jqGrid({
url: '/Home/GridData/',
datatype: 'json',
mtype: 'GET',
colNames: ['Id', 'Votes', 'Title'],
colModel: [
{ name: 'Id', index: 'Id', width: 40, align: 'left' },
{ name: 'Votes', index: 'Votes', width: 40, align: 'left' },
{ name: 'Title', index: 'Title', width: 200, align: 'left'}],
pager: jQuery('#pager'),
rowNum: 10,
rowList: [5, 10, 20, 50],
sortname: 'Id',
sortorder: "desc",
viewrecords: true,
imgpath: '/scripts/themes/smoothness/images',
caption: 'My first grid'
});
});
Seems reasonable right? Why on earth would it download the page instead of redirecting to it? What on Earth could I possible be doing wrong here. Well plenty I guess, but I think I am just missing something simple.
Ok. I think I figured it out. I don’t want to redirect to an action. The action returns a Json data type and that is just text so the browser is just going to attempt to download it. I want to redirect to a view… So what I did is make a helper function that returns a json data and I do redirect to the dataGrid controller. But the Json script now calls the helper function and that worked… Except for a Javascript error which I have know Idea how to solve (It is basically a formatting error so if I click past it, it will render).
Thanks for the help anyway guys.
Derek
P.S here is my coded solution incase you wanted to check it out for yourself if you are having the same problem…
jqGrid call
Here are my actions inside the controller.
As you can see after I redirect to GridData(), which calls the griddata view, jqGrid then calls GetData(). GetData then returns Json data which is rendered in the view…
If anyone has a better way to do this, please reply. But thanks for the help.
Derek