2011-06-02 23 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

相關問題