【问题标题】:Using jEditable with ASP.NET MVC (POSTing)将 jEditable 与 ASP.NET MVC 一起使用(发布)
【发布时间】:2010-11-17 23:58:42
【问题描述】:

我了解使用 jEditable (http://www.appelsiini.net/projects/jeditable) 您可以进行就地编辑并将更改的信息发布到 URL。

我的 ASP.NET MVC 视图显示了一堆模型信息,我希望这些信息可以就地编辑。目前,我有两个视图 - 一个文本表示和一个编辑视图,其中一个表单完全发布,然后我的控制器操作将整个对象(从表单元素名称组装)作为参数,更新对象并返回到文本- 仅查看。

但是,当我切换到 jEditable 时,我只会使用文本视图并一次发布一个项目,而不是整个对象。我如何构建一个可以接受 jEditable 正在发布的内容的单个控制器操作,然后将其放入对象的适当属性中?

【问题讨论】:

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


    【解决方案1】:

    有一些不错的sample code here:

    $("#myTextBox").editable('<%=Url.Action("UpdateSettings","Admin") %>', {   
               submit: 'ok',   
               cancel: 'cancel',   
               cssclass: 'editable',   
               width: '99%',   
               placeholder: 'emtpy',   
               indicator: "<img src='../../Content/img/indicator.gif'/>"  
           });  
    
    
    [AcceptVerbs("POST")]   
    public ActionResult UpdateSettings(string id, string value)   
    {   
        // This highly-specific example is from the original coder's blog system,
        // but you can substitute your own code here.  I assume you can pick out
        // which text field it is from the id.
        foreach (var item in this.GetType().GetProperties())   
        {   
    
            if (item.Name.ToLower().Equals(id, StringComparison.InvariantCultureIgnoreCase))   
                item.SetValue(Config.Instance, value, null);   
        }   
        return Content(value);   
    } 
    

    您可能还需要这个:
    http://noahblu.wordpress.com/2009/06/17/jeditable-note-dont-return-json-and-how-to-return-strings-from-asp-net-mvc-actions/

    【讨论】:

    • 但是如果我有一个大模型,比如说有 20 个属性怎么办。我需要 20 种不同的控制器方法吗?或者,如果我在您的示例中使用通用 id/value 参数对,如何让值自动更新正确的对象属性?
    • 原始代码示例中有一个循环......这一定是循环的用途。我在代码示例中恢复了循环。您可能想要查看原始博客条目。你的网页上真的有 20 个小文本编辑器吗?这似乎很不寻常。
    • 您需要做一些工作才能将您从文本编辑器获得的值与您的数据库模型结合起来。这似乎与标准视图模型的工作方式不同,因此您不会免费获得自动模型更新。
    • 您可以尝试使用 ID 和值将其添加到请求对象(它是可编辑的,对吗?)。然后调用 UpdateModel() 并传递对象——让 UpdateModel 担心反射和所有这些。
    • 撞到这个因为我也有一个大的、可编辑的模型。这对你有什么影响?谢谢
    【解决方案2】:

    这是我通过反思所做的:

    查看:

    $(".edit").editable('<%=Url.Action("UpdateEventData","Event") %>', {
                    submitdata: {eventid: <%=Model.EventId %>},
                    tooltip: "Click to edit....",
                    indicator: "Saving...",
                    submit : "Save",
                    cancel : "Cancel"
                });
    

    控制器:

    public string UpdateEventData(int eventid, string id, string value)
        {
            //load the event
            var evt = Repository.GetEvent(eventid);
    
            //UpdateModel;
            System.Reflection.PropertyInfo pi = evt.GetType().GetProperty(id);
            if (pi==null)
                return "";
            try
            {
    
                object newvalue = Concrete.HelperMethods.ChangeType(value, pi.PropertyType);
    
                pi.SetValue(evt, newvalue, null);
                Repository.Save();
    
            }
            catch (Exception ex)
            {
                //handle errors here
            }
    
            return pi.GetValue(evt, null).ToString();
    
        }
    

    “HelperMethods.ChangeType”方法是我从http://aspalliance.com/author.aspx?uId=1026 获得的Convert.ChangeType 的更健壮版本(因此它可以处理可空值)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-04
      • 2010-09-09
      • 2011-06-26
      • 2014-09-27
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多