Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Asp mvc jquery - in place edit
- <div style="border: 1px solid #dddddd;">
- <div id="wrap">
- <h3 id="cText">@Model.CommentText</h3>
- <a id="runEdit" href="#" >Edit</a>
- </div>
- </div>
- e.preventDefault();
- var txt = $('#cText').text();
- $('#cText').remove();
- $('#wrap').prepend('<textarea>' + txt + '</textarea>');
- $('#wrap').append('<input type="submit" value="Ok" />');
- $('#wrap').append('<input type="submit" value="Cancel" />');
- $('#editButton').live('click', function (e) {
- e.preventDefault();
- var container = $(this).closest('.commentWrap');
- var itemId = container.attr('id');
- var nestId = '#' + itemId;
- var txt = $('#commentTextValue').text();
- $(nestId + ' #commentTextValue').remove();
- $(nestId + ' #editButton').remove();
- $(nestId).prepend('<textarea id="editArea">' + txt + '</textarea>');
- $(nestId).append('<input type="submit" value="Ok" class="btnOk" />');
- })
- $('.btnOk').live('click', function (e) {
- e.preventDefault();
- var container = $(this).closest('.commentWrap');
- var itemId = container.attr('id');
- var text = container.find('textarea').val();
- var nestId = '#' + itemId;
- //alert(nestId);
- $.ajax({
- url: '/Comment/SaveComment',
- data: JSON.stringify({ CommentText: text, CommentId: itemId }),
- type: 'post',
- contentType: 'application/json',
- success: function (data) {
- if (data.success === true) {
- //alert(data.message); // do show/hide stuff here instead of the alert
- $(nestId + ' #editArea').remove();
- $(nestId + ' .btnOk').remove();
- $(nestId).append('<h3 id="commentTextValue">' + data.message + '</h3>');
- $(nestId).append('<a id="editButton" href="#">Edit</a>');
- }
- }
- });
- });
- </script>
- <div style="border: 1px solid #dddddd;">
- @Html.ActionLink(@Model.Author, "SomeAction")
- <div class="commentWrap" id="@Model.CommentId">
- <p id="commentTextValue">@Model.CommentText</p>
- <a id="editButton" href="#">Edit</a>
- </div>
- </div>
- <div class="wrap" id="123"></div>
- <input type="submit" class="btnOk" value="Ok"/>
- $('.btnOk').live('click',function(e){
- e.preventDefault();
- var container = $(this).closest('.wrap');
- var itemId = container.attr('id');
- var text = container.find('textarea')[0].val();
- $.ajax({
- url: '/mycontroller/savecomment',
- data: JSON.stringify({comment: text, id:itemId}), // using JSON2, see below
- type: 'post',
- contentType: 'application/json',
- success: function(data){
- if(data.success===true){
- alert(data.message); // do show/hide stuff here instead of the alert
- }
- }
- });
- });
- public ActionResult SaveComment(string text, int id)
- {
- //save your comment for the thing with that id
- var result = new {success = true, message = "saved ok"};
- return Json(result, JsonRequestBehavior.AllowGet);
- }
- <div style="border: 1px solid #dddddd;">
- <div id="wrap">
- <h3 id="cText">@Model.CommentText</h3>
- <a id="runEdit" href="#" >Edit</a>
- </div>
- <div id="editPanel" style="display:none;">
- <form action="@Url("Edit", "Whatevercontroller")">
- <textarea name="CommentText">@CommentText</textarea>
- <input type="submit" value="Ok" />
- <a href="#" id="cancelEdit">Cancel</a>
- </form>
- </div>
- </div>
- function StartEdit() {
- $("#wrap").css("display", "none");
- $("#editPanel").css("display", "block");
- }
- function CancelEdit() {
- $("#wrap").css("display", "block");
- $("#editPanel").css("display", "none");
- }
- var d = "poo=1&bar=2";
- $.ajax({
- type: "POST",
- url: "/home/myaction",
- data: d,
- success: function (r) {
- alert(r.data);
- },
- complete: function () {
- alert("I am done!");
- },
- error: function (req, status, error) {
- //do what you need to do here if an error occurs
- alert("error");
- }
- });
Add Comment
Please, Sign In to add comment