Using jEditable with ASP.NET MVC (POSTing)

I understand that with jEditable ( http://www.appelsiini.net/projects/jeditable ) you can do in-place editing and POST changed information in the URL.

My ASP.NET MVC view displays a ton of model information that I would like to make editable in place. Currently, I have two views: one text view and one editable view, in which the form is fully POSTED, and then my controller action takes the entire object (assembled from the names of the form elements) as a parameter, updates the object and returns the text - only view .

However, when I switch to jEditable, I would only use the text view and POST for one element at a time, and not the whole object. How can I create one controller action that can accept that jEditable is POSTing and then put it in the corresponding property of my object?

+3
source share
2 answers

Here are some good 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 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);   
} 

You may also need the following:
http://noahblu.wordpress.com/2009/06/17/jeditable-note-dont-return-json-and-how-to-return-strings-from-asp-net-mvc- actions /

+8
source

Here is what I do through reflection:

View:

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

Controller:

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" Convert.ChangeType( nullables), http://aspalliance.com/author.aspx?uId=1026.

+1

Source: https://habr.com/ru/post/1714679/