asp.net mvc + jEditable - 在数据库中保存已编辑的单元格数据

时间:2011-07-20 07:53:29

标签: jquery asp.net-mvc-3 datatables jeditable

我正在尝试使用jquery DataTables插件来显示我的db表中的详细信息,并使用Jeditable允许用户内联编辑每个单元格。编辑后的数据应该回发并保存在数据库中。我遇到了一个与我的场景非常相似的示例:http://naspinski.net/post/Inline-AJAX-DropDown-and-Text-Editing-with-AspNet-MVC-and-jQuery.aspx我尝试按照此作为指南实现。

但是,我在这里面临一些问题:

  1. 何时以及如何使用Url.Content()以及应该传入和返回的内容?

  2. 尝试编辑表格单元格时出错:[MissingMethodException]:没有为此对象定义无参数构造函数。

  3. 我知道我在这里做了一些非常错的事,但我无法澄清我的怀疑。 这是我用来使我的单元格可编辑的脚本:

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

    我用控制器操作将编辑后的数据保存到db:

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

    在这里真的需要一些帮助...欣赏它......

2 个答案:

答案 0 :(得分:0)

第一个问题:

Url.Content()应该用于提供静态文件,例如JS或CSS,如

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

它将返回此静态文件的直接URL。从~开始将确保使用正确的基础目录(即,如果您在虚拟目录中运行应用程序)。

第二个问题:

您的操作方法必须返回ActionResult才能识别为操作。它可以是参数更少,因为您可以访问HttpContext作为控制器类的属性。

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

答案 1 :(得分:0)

我以某种方式完成它虽然它仍然需要大量的后续工作.. 我更改我的控制器以返回ActionResult并传递2个参数(id和value),并且我在我的视图中坚持使用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'
    });

谢谢!