2011-06-02 85 views
0

这可能是堆栈溢出时这里最愚蠢的问题。但是我正在从我正在使用的一些代码中获得最奇怪的结果。我试图让jqGrid在我的MVC 2应用程序中工作。为什么我要下载一个aspx页面,而不是在MVC 2中导航到这个页面

我的家庭控制器有一个索引和GridData的操作方法... GridData需要4个参数,其中2个不能为空,所以我添加一个defalutValue属性值为1给他们。该指数控制器重定向到的GridData操作方法至极然后打开了的GridData看法......我也不在这个函数返回的看法,但我回一个JSON变量...

[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); 
    } 
} 

这里是我的大多数Javascript代码。

<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' 
    }); 
}); 

似乎是合理的吗?为什么它会下载页面而不是重定向到它?我可能会在这里做错什么。我想我的确很多,但我认为我只是缺少一些简单的东西。

回答

0

好的。我想我明白了。我不想重定向到某个操作。该操作返回一个Json数据类型,这只是文本,所以浏览器只是试图下载它。我想重定向到一个视图...所以我做了一个帮助函数,返回一个JSON数据,我重定向到dataGrid控制器。但Json脚本现在调用辅助函数,并且工作...除了我已经知道的Javascript错误想法如何解决(这基本上是一个格式错误,所以如果我点击过去,它会呈现)。

感谢无论如何帮助家伙的帮助。

Derek

P.这儿是柜面你想检查自己出来,如果您有同样的问题,我的编码的解决方案...... 的jqGrid调用

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

这里是我的控制器内的行动。

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"); 
    } 

正如你可以看到我后重定向到的GridData(),它调用的GridData视图,jqGrid的然后调用的GetData()。 GetData然后返回在视图中呈现的Json数据...

如果有人有更好的方法来做到这一点,请回复。但感谢您的帮助。

Derek

0

我认为你的问题是在这里:

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

也许你正在试图重新定向您的看法,对此实际上返回一个JSON结果的动作。

尝试REM这些行,看看会发生什么。

UPDATE:

在我看来,你误认为MVC的工作方式。 解决你的问题,你可以尝试清洁你的动作视图,以便它看起来像这样:

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

然后,你需要有相关的视图,它必须包含您的jqGrid。 然后你需要有一个简单的返回一些json数据的动作。 该动作将始终由您的jqGrid调用(并且仅)。 这正是你的GridData所做的。 您不会重定向到从jqGrid调用的GridData原因。

Craig Stuntz对此有不错的评价tutorial。 这one也可能有帮助。

您可以下载我的示例代码here

+0

很确定不是这样。但是这确实给了我一个想法。我重定向到特定的控制器和操作。不幸的是我得到了同样的结果。我保证,如果我尝试过,我永远无法做到这一点。 – SoftwareSavant 2011-06-02 17:09:04

+0

@ user729820:检查你的路由。 – LeftyX 2011-06-02 17:20:16

+0

一直在看我的路由。关于我的路由引擎究竟会让我下载页面而不是重定向到它? – SoftwareSavant 2011-06-02 18:11:18

相关问题