0

我在需要String视图参数的控制器中创建了一个动作。鉴于我有一个超链接和onclick事件,我想调用一个jQuery函数来将该值发送到操作。

$(function () {
            debugger;
            $('.delete-icon').click(function () {
               //alert($(this.id));
                var answer = confirm('Do you want to remove this image?');
                var id1 = $('.delete-icon').data('val1');
                var jsonval = JSON.stringify(id1);
                alert(jsonval);
                if (answer) {
                    $.ajax({
                        type: 'POST',
                        contentType: "application/json;charset=utf-8",                        
                        //data: { id: jsonval },
                        //url:'@Url.Action("ImageDelete", "AdminNew")',
                        url: '/AdminNew/ImageDelete/' + jsonval,
                        dataType: 'json',
                        //traditional: true,                                                
                        success: function (ajaxresult) {
                            $("#" + jsonval).remove();
                        },
                        failure: function (ajaxresult, status) {
                            console.log(ajaxresult)
                        }
                    });
                                       return false;
                }
                return false;
            });
        });

<table>
                                <% foreach (var imagealbum in ViewBag.ImageList)
                                   { %>
                                <tr id="<%:imagealbum.frame_image_id %>">
                                    <td class="checkbox">
                                        <input id="chk" type="checkbox" checked="checked" /></td>
                                    <td class="image">
                                        <img src="<%:imagealbum.image_path%>" width="50" height="50" />
                                    </td>
                                    <td class="image-name"><%:imagealbum.image_path%> </td>
                                    <td class="image-delete">
                                        <a href="<%:Url.Action("ImageDelete", "AdminNew", new { id = imagealbum.frame_image_id})%>" class="delete-icon" data-val1="<%:imagealbum.frame_image_id%>"></a>     

                                    </td>
                                </tr>
                                <%} %>
                            </table>
4

3 回答 3

0

您不需要传递额外的值,因为所需的值已经在链接的 href 中。

$(function () {
    $('.delete-icon').click(function (e) {
        e.preventDefault();

        var answer = confirm('Do you want to remove this image?');
        var deleteIcon = $(this);
        var url = deleteIcon.prop('href');
        if (answer) {
            $.ajax({
                type: 'POST',
                contentType: "application/json;charset=utf-8",                        
                url: url,
                dataType: 'json',
                success: function (ajaxresult) {
                    deleteIcon.closest('tr').remove();
                },
                failure: function (ajaxresult, status) {
                    console.log(ajaxresult)
                }
            });
        }
    });
});
于 2013-06-01T11:33:13.653 回答
0

试试下面

动作方法

public ActionResult SendStream(string path)
{

}

jQuery/JSON

$.ajax({
    url: url,                                   //Without appending Json value
    data: JSON.stringify({ path: "Some Val" }), //Parameters
    type: 'POST',     
    contentType: 'application/json, charset=utf-8',
    dataType: 'json'
}).done(function (data) {                       //Success Callback

}).fail(function (data) {                       //Failed Callback        

}).always(function(data) {                      //Request completed Callback

});
于 2013-06-01T11:45:34.810 回答
0

首先创建一个 ActionResult 如下。

公共 ActionResult SendStream(字符串路径){

}

现在,

$(function () {
            $('.delete-icon').click(function () {
                var answer = confirm('Do you want to remove this image?');
                var id1 = $('.delete-icon').data('val1');
                var jsonval = JSON.stringify(id1);
                if (answer) {
                    $.ajax({
                        type: 'POST',
                        contentType: "application/json;charset=utf-8",                        
                        url: '/Shared/SendStream?path=' + jsonval,
                        dataType: 'json',
                        success: function (ajaxresult) {
                            $("#" + jsonval).remove();
                        },
                        failure: function (ajaxresult, status) {
                            console.log(ajaxresult)
                        }
                    });
                                       return false;
                }
                return false;
            });
        });

请尝试上面的示例并检查。

于 2013-06-01T12:02:07.367 回答