簡體   English   中英

如何使用 javascript 從視圖中將 model 數據發布到 controller

[英]How to post model data to controller from view using javascript

我需要根據視圖中當前 model 中的值檢查數據庫中的一些數據,以便將頁面重定向到所有用戶或顯示一條消息以等待其他用戶,我還需要每隔幾秒調用一次 function檢查其他人是否已提交。

//我嘗試過使用 $(this).serialize(); 但它發送一個空的 model

 function yourFunction() {
    var model = $(this).serialize();       
 $.post('/Home/getmessage', model, function (response) {
    });
    if(ViewBag.submit==true)
    window.location.href = "http://localhost:2537/Home/Score"
  setTimeout(yourFunction, 5000);
  }

嘗試這個,

function yourFunction() {
var data = JSON.stringify($(this).serialize());
        $.ajax({
            type: "POST",
            url: "/Home/getmessage",
            cache: false,
            data: data,
            contentType: "application/json",
            success: function (response) {
                if (response.Success)
                {                     

                }
            }
        });
 if(ViewBag.submit==true)
 window.location.href = "http://localhost:2537/Home/Score"
 setTimeout(yourFunction, 5000);

};

嘗試這個:

function yourFunction(){
var data = JSON.stringify($(this).serialize());
        $.ajax({
            type: "POST",
            url: "/Home/getmessage",
            cache: false,
            data: data,
            contentType: "application/json",
            success: function (response) {
                if (response.Success)
                {                     

                }
            }
        });
};

if(ViewBag.submit==true)
 setTimeout(yourFunction, 5000);

public ActionResult getmessage()
{
    return RedirectToAction("score");
}

暫無
暫無

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

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