簡體   English   中英

ASP MVC jQuery-就地編輯

[英]Asp mvc jquery - in place edit

我那里有一些數據的div列表:

<div style="border: 1px solid #dddddd;">        
        <div id="wrap">
        <h3  id="cText">@Model.CommentText</h3>   
        <a id="runEdit" href="#" >Edit</a> 
        </div>
</div>

當用戶單擊runEdit鏈接時,我將根據以下內容進行編輯:

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" />');

問題是我在這里在JavaScript中添加了這兩個按鈕。 但是我不知道如何在此按鈕上附加一些控制器動作?

這里的問題是,如果我寫5條評論。 然后單擊“編輯”,我得到5個編輯表單。

$('#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添加一個itemid,然后將id = wrap轉換為一個類,因為其中有多個。

<div class="wrap" id="123"></div>

這樣,您便可以引用正在編輯的項目的ID。

您還應該將一個類添加到您在頁面上注入的提交按鈕fx上:

<input type="submit" class="btnOk" value="Ok"/>

然后,您可以連接javascript:

$('.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

        }
     }
   });

}); 

注意:下載json2庫並將其添加到腳本引用中-這是進行json序列化的好方法。 https://github.com/douglascrockford/JSON-js

在您的控制器中,您必須添加一個操作方法來處理請求:

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

馬克的答案是正確的。 以此包圍您的代碼。 但是,我強烈建議您盡可能多地使用“ html in html”而不是JavaScript。

上面的代碼可以翻譯成更好的形狀,像這樣,

<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>

和js將是

function StartEdit() {
    $("#wrap").css("display", "none");
    $("#editPanel").css("display", "block");
}
function CancelEdit() {
    $("#wrap").css("display", "block");
    $("#editPanel").css("display", "none");
}

這種方法的優點是在這種情況下不會生成太多的DOM元素。 否則,您的JavaScript將會變得絕對難以管理。

您必須在您的文本區域周圍放置一個form標簽,並通過@ Url.Action幫助程序將表單的操作設置為所需的操作。

您需要對控制器操作進行Ajax調用。 請參考以下鏈接:

http://tugberkugurlu.com/archive/working-with-jquery-ajax-api-on-asp-net-mvc-3-0-power-of-json-jquery-and-asp-net-mvc-partial-意見

您將在此處找到樣本。

基本上,您需要做的如下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM