weixin_33713503 2017-02-18 21:16 采纳率: 0%
浏览 40

使用AJAX,CSHTML发布表单

trying to understand all this about AJAX, first of all I wanted to know how to refresh a page and keep my position on the page, which was possible, however, that was not the case on form post, that just jumped me right back to the top.

So after searching around on how to solve that, posting with AJAX seems to be my solution, I just can't seem to get all of it.

<form method="post" action="~/getAJAX.cshtml" id="ajaxform">
        <input type="text" name="kg" id="kg"  />
        <input type="submit" />
</form>
<script type="text/javascript">
    $(function () {
        $('#ajaxform').submit(function (event) {
            event.preventDefault(); // Prevent the form from submitting via the browser
            var form = $(this);
            $.ajax({
                type: form.attr('method'),
                url: form.attr('action'),
                data: form.serialize()
            }).done(function (data) {
                // Optionally alert the user of success here...
            }).fail(function (data) {
                // Optionally alert the user of an error here...
            });
        });
    });
</script>

This is the code I have so far. The thing I do not understand is what exactly should be on my "action" page?

At the moment I put this on my action page.

var db = Database.Open("Database");

var getKG = Request.Form["kg"];

var query = "SELECT * FROM Test WHERE kg = @0";
db.Execute(query, getKG);

This is just a test, I don't really know what to expect or anything, I would like to show the results on the page I post from, any guiding for this please?

Note that this is not a MVC project, therefore my problems with finding any good solutions or help, it's just normal CSHTML files.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
    • ¥15 关于arduino编程toCharArray()函数的使用
    • ¥100 vc++混合CEF采用CLR方式编译报错
    • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
    • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
    • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
    • ¥50 如何openEuler 22.03上安装配置drbd
    • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
    • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
    • ¥15 Windows11, backspace, enter, space键失灵