是否有可能WebMatrix的CRUD在同一页上使用Ajax有可能、WebMatrix、Ajax、CRUD

2023-09-10 17:38:10 作者:易生只爱烊

我想获得的想法有关,什么是samepage与阿贾克斯在WebMatrix的剃刀语法,使CRUD操作(更新 - 编辑 - 插入)最快和安全的方式。是否有可能做到这一点CRUD操作与阿贾克斯同一个剃须刀页面没有GET,POST web服务或其他剃须刀页?

I would like to get idea about, what is the fastest and secure way to make CRUD operations (Update-Edit-insert) in samepage with Ajax in WebMatrix Razor Syntax. Is it possible to do this CRUD Operations in same razor page with ajax without GET-POST webservice or other razor page?

我试图使用jQuery Ajax来后从其他剃刀页面与输出类型JSON和同样使用WCF web服务的数据。但他们没有真正满足我,因为我需要的另一页为我的数据。

I tried using jquery ajax to Post-Get data from other Razor pages with Output type Json and also using WCF Webservice. But they didnt really satisfy me, because in all i need another page to serve my data.

推荐答案

是的,你可以做到这一点。每个单独的CRUD操作将必须包在它自己的条件块。条件可以测试,看是否有特定的名称/值对已经通过AJAX(如行动=删除行动=更新)。当回返回值AJAX调用,请确保的ContentType 响应的设置是否正确。

Yes, you can do this. Each separate CRUD operation would have to be wrapped in its own conditional block. The condition can test to see if a particular name/value pair has been sent to the server via AJAX (e.g. action=delete or action=update). When returning values back to AJAX call, make sure that the ContentType of the Response is set correctly.

下面是一个工作的例子 -

Here's a working example -

@{
    if(IsAjax){
        switch(Request["action"]){
            case "create" :
                Response.Write("You are trying to create something");
                Response.End();
                break;
            case "read" :
                Response.Write("What would you like to read?");
                Response.End();
                break;
            case "update" :
                Response.Write("Something is being updated");
                Response.End();
                break;
            case "delete" :
                Response.Write("This is a delete");
                Response.End();
                break;
        }
    }
}

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script src="~/Scripts/jquery-1.8.3.min.js" type="text/javascript"></script>
        <script>
            $(function () {
                $('.crud').click(function () {
                    $.get('/SinglePage/?action=' + $(this).attr('id'), function (message) {
                        alert(message);
                    })
                })
            })
        </script>
    </head>
    <body>
        <button id="create" class="crud">Create</button>
        <button id="read" class="crud">Read</button>
        <button id="update" class="crud">Update</button>
        <button id="delete" class="crud">Delete</button>
    </body>
</html>

如果有的话,这说明多么糟糕这个方法可能是维护等方面我总是使用单独的CSHTML文件为每个数据操作自己,

If anything, this illustrates how bad this approach might be in terms of maintenance etc. I would always use separate cshtml files for each data operation myself,