2013-07-25 18 views
0

在MVC4應用程序Create(後)的操作中,我想通過int類型列表查看是否發生錯誤。然後,從那裏通過ajax post將其傳遞給其他方法。所以,TempData,ViewDataViewBag不幫我。如何從動作傳遞int列表來查看,然後從視圖到mvc 4中的方法?

public ActionResult Create(CreateModel model) 
     { 
      if(hasCustomError) 
      { 
        List<int> selectedItems = new List<int>() { 1, 2, 8 }; //for example. 
        ViewBag.VB = selectedItems; 

        //ViewData["VD"] = selectedItems; 
        //TempData["TD"] = selectedItems; 

        return View(model); 
      } 

      return RedirectToAction("Index"); 
     } 

return View(model);後,selectedItems列表傳遞給Create.cshtml視圖。在這裏我檢查了它的價值。但在這裏,我要通過ajax後傳遞名單GetTreeData方法:

@{ 
    if (ViewBag.VB != null) 
     { 
      TempData["SelectedItems"] = ViewBag.VB as List<int>; 
     } 
} 


    $("#myTree").jstree({ 
     json_data: { 
      "ajax": { 
       url: "@Url.Action("GetTreeData", "MyController")", 
       type: "POST", 
       dataType: "json", 
       contentType: "application/json charset=utf-8" 
      } 
     }, 
     checkbox: { 
      real_checkboxes: true, 
      checked_parent_open: true 
     }, 
     plugins: ["themes", "json_data", "ui", "checkbox"] 
    }); 

MyController,在GetTreeData方法TempData["SelectedItems"]null

public string GetTreeData() 
     { 
      List<int> selecteds = null; 
      if (TempData["SelectedItems"] != null) 
      { 
       selecteds = TempData["SelectedItems"] as List<int>; 
       TempData["SelectedItems"] = null; 
      } 
      ...................................... 
     } 

我試過這一切(TempData,ViewData和ViewBag)。沒有改變。

如何從動作傳遞該列表來查看,然後從該視圖到方法?

+0

我找到了解決方案。首先,我將int列表轉換爲字符串列表爲「1,2,8」,然後, url:「@ Url.Action(」GetTreeData「,」MyController「,new {param = stringList})」, –

回答

1

創建一個視圖模型,在該視圖模型設置您現在使用的領域模型,並添加一個額外的字段該列表

+0

yes I have不是我看到的其他方式。我不會找到任何方式,這樣做。 –

相關問題