使用jEditable和ASP.NET MVC(POSTing)

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

我的ASP.NET MVC视图显示了一堆模型信息,我想在其中进行就地编辑。 目前,我有两个视图 – 一个文本表示和一个编辑视图,其中一个表单完全POST,然后我的控制器操作将整个对象(从表单元素名称汇编)作为参数,更新对象并返回到文本 – 只能看。

但是,当我切换到jEditable时,我只会使用文本视图并一次POST一个项目,而不是整个对象。 我怎么能构建一个单独的控制器动作,可以采取jEditable的POST,然后将其放入我的对象的相应属性?

这里有一些非常好的示例代码 :

$("#myTextBox").editable('<%=Url.Action("UpdateSettings","Admin") %>', { submit: 'ok', cancel: 'cancel', cssclass: 'editable', width: '99%', placeholder: 'emtpy', indicator: "" }); [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/

这是我通过反思做的事情:

视图:

 $(".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的更强大版本(因此它可以处理nullables)。