2011-07-20 73 views
0

我試圖使用jquery DataTables插件來顯示我的數據庫表中的詳細信息,並使用Jeditable允許用戶編輯每個單元格內聯。編輯的數據應回發並保存在數據庫中。我遇到了一個與我的情況非常相似的示例:http://naspinski.net/post/Inline-AJAX-DropDown-and-Text-Editing-with-AspNet-MVC-and-jQuery.aspx,我試圖按照以下指示進行實施。asp.net mvc + jEditable - 將編輯過的單元格數據保存到數據庫中

不過,我在這裏面對一些問題:

  1. 何時以及如何我應該使用Url.Content(),什麼也應傳遞和返回?

  2. 嘗試編輯表格單元格時出現錯誤:[MissingMethodException]:沒有爲此對象定義無參數的構造函數。

我知道我在這裏做了一些非常錯誤的事情,但我無法清除我的疑惑。 這裏是我曾經讓我的電池可編輯的腳本:

$(function() { 

    // Initialize a data table 
    var myTable = $('#example').dataTable({ 
     // To use themeroller theme 
     "bJQueryUI": true 
    }); 

    // Make every cell editable 
    $('td', myTable.fnGetNodes()).editable('@(Url.Action("Edit", "Home"))', 
    { 
     indicator: 'saving...', 
     tooltip: 'click to edit...', 
     style: 'inherit', 
     placeholder: 'click to edit' 
    }); 
}); 

而控制器動作我曾經編輯過的數據保存到數據庫:

[HttpPost] 
    public void Edit(HttpContext context) 
    { 
     string elementId = context.Request.Form["id"]; 
     string fieldToEdit = elementId.Substring(0, 4); 

     //now take anything after those 4 and it is the Id: 
     int idToEdit = Convert.ToInt32(elementId.Remove(0, 4)); 

     // the value is simply a string: 
     string newValue = context.Request.Form["value"].Trim(); 

     var food = dbEntities.Foods.Single(i => i.FoodID == idToEdit); 

     switch (fieldToEdit) 
     { 
      case "name": food.FoodName = newValue; break; 
      case "amnt": food.FoodAmount = Convert.ToInt32(newValue); break; 
      case "sdat": food.StorageDate = Convert.ToDateTime(newValue); break; 
      case "edat": food.ExpiryDate = Convert.ToDateTime(newValue); break; 
      case "type": food.FoodTypeID = Convert.ToInt32(newValue); break; 
      case "cont": food.ContainerID = Convert.ToInt32(newValue); break; 
      default: throw new Exception("invalid fieldToEdit passed"); 

     } 
     dbEntities.SaveChanges(); 


     context.Response.Write(newValue); 
    } 

真的需要一些幫助這裏...欣賞它...

回答

0

第一個問題:

Url.Content()應該用於服務靜態文件,即JS或CSS像

Url.Content("~/Scripts/jquery.js") 

它會返回一個直接的URL到這個靜態文件。從~開始將確保使用正確的基本目錄(即,如果您正在虛擬目錄中運行您的應用程序)。

第二個問題:

你的操作方法必須返回一個ActionResult被認定爲一個動作。它可以減少參數,因爲您可以訪問HttpContext作爲控制器類的屬性。

[HttpPost] 
public ActionResult Edit() 
{ 
    string elementId = this.HttpContext.Request.Form["id"]; 
} 
0

我莫名其妙地把它做但仍需要後續大量的工程.. 更改我的控制器返回的ActionResult和傳遞2個參數(ID和值),我堅持Url.Action我查看..它的工作,雖然我不知道我做錯了什麼..

這裏是我的控制器的一部分:

[HttpPost] 
    public ActionResult Edit(string id, string value) 
    { 
     string elementId = id; 
     string fieldToEdit = elementId.Substring(0, 4); 

     //now take anything after those 4 and it is the Id: 
     int idToEdit = Convert.ToInt32(elementId.Remove(0, 4)); 

     // the value is simply a string: 
     string newValue = value; 

和腳本:

// Make every cell editable 
    $('td', myTable.fnGetNodes()).editable('@(Url.Action("Edit", "Home"))', 
    { 
     indicator: 'saving...', 
     tooltip: 'click to edit...', 
     style: 'inherit', 
     placeholder: 'click to edit' 
    }); 

謝謝!

相關問題