简体   繁体   English

如何使用 javascript 从视图中将 model 数据发布到 controller

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

I need to check on some data from the database based on values in my current model in the view in order to redirect the page to all users or show a message to wait for the others also i need the function to be called every few seconds to check if the others submitted yet.我需要根据视图中当前 model 中的值检查数据库中的一些数据,以便将页面重定向到所有用户或显示一条消息以等待其他用户,我还需要每隔几秒调用一次 function检查其他人是否已提交。

//i have tried using the $(this).serialize(); //我尝试过使用 $(this).serialize(); but it sends an empty model但它发送一个空的 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);
  }

try this,尝试这个,

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

}; };

Try this:尝试这个:

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