开发者

Why do I download an aspx page instead of just navigating to it in MVC 2

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, cel开发者_运维问答l = 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

jQuery(document).ready(function () {
    jQuery("#list").jqGrid({
        url: '/Home/GetData/',
        datatype: 'json',

Here are my actions inside the controller.

 public JsonResult GetData(string sidx, string sord, [DefaultValue(1)] int page, [DefaultValue(3)] int row)
    {
        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);
    }

    public ActionResult GridData() 
    {
        return View("GridData");
    }

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


I reckon your problem is here:

if (nextButton != null)
            return RedirectToAction("GridData");

Probably you're trying to redirect your view to the action which actually returns a JSON result.

Try to REM those lines and see what happens.

UPDATE:

It seems to me that you're misunderstanding the way that MVC works. To solve your problem you can try to clean your action View so it looks like this:

public ActionResult Index()
    {
        return View();
    }

Then you need to have the associated view, which must contain your jqGrid. Then you need to have an action which simply returns some json data. That action will always be called (and only) by your jqGrid. That's exactly what your GridData does. You do not redirect to GridData cause that is called from jqGrid.

Craig Stuntz has got a good tutorial about that. This one might help too.

You can download my sample code here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜