【发布时间】:2011-07-20 07:53:28
【问题描述】:
我正在尝试使用 jquery DataTables 插件来显示我的数据库表中的详细信息,并使用 Jeditable 允许用户内联编辑每个单元格。编辑后的数据应回传并保存在数据库中。我在这里遇到了一个与我的场景非常相似的示例:http://naspinski.net/post/Inline-AJAX-DropDown-and-Text-Editing-with-AspNet-MVC-and-jQuery.aspx,我尝试按照它作为指南来实现。
但是,我在这里遇到了一些问题:
应该在什么时候以及如何使用 Url.Content() 以及应该传入和返回什么?
尝试编辑表格单元格时出现错误:[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);
}
这里真的需要一些帮助...感谢它...
【问题讨论】:
标签: jquery asp.net-mvc-3 datatables jeditable