【问题标题】:asp.net mvc + jEditable - save edited cell data in databaseasp.net mvc + jEditable - 将编辑后的单元格数据保存在数据库中
【发布时间】: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,我尝试按照它作为指南来实现。

但是,我在这里遇到了一些问题:

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

这里真的需要一些帮助...感谢它...

【问题讨论】:

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


    【解决方案1】:

    第一个问题:

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

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

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

    第二个问题:

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

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

    【讨论】:

      【解决方案2】:

      我以某种方式完成了它,尽管它仍然需要很多后续工作.. 我将控制器更改为返回 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'
          });
      

      谢谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-06
        • 1970-01-01
        • 2018-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-17
        • 2019-09-29
        相关资源
        最近更新 更多