如何从控制器在MVC中显示Jquery消息



我正在使用MVC,我正在检查后端(控制器)的条件。我想在一个Jquery模态框中显示消息,这将从后端(控制器)触发。

谁能告诉我怎么做这件事?

我已经尝试使用以下代码,但这给了我一些消息:无效参数。

string scriptstring = "$(function(){initializedialog();showDialog("" + "Please answer to all the Question." + "");});";
            ScriptManager.RegisterStartupScript(this, typeof(Page), "test", scriptstring , true);

你能告诉我如何在MVC中使用上面的语句吗

谢谢

您可以使用ajax这样做,因为ssilas777已被注释掉。

使用下面的$。ajax代码。

$(function(){
    $.ajax({
        url:'@Url.Action("Index","Home")',
        type: 'GET',
        dataType:'json',
        success:function(data){
            //Here you will get the data.
            //Display this in your Modal popup
        },
        error: function(data){
        }
    });
});

控制器代码

public ActionResult Index(){
    //do some stuff
    string Message="your message here !";
    return Json(Message,JsonRequestBehaviour.AllowGet);
}

在Controller中设置ViewBag Flag:

 ViewBag.Login = "No";

然后在查看检查文档。准备好是否设置,并在那里给出您的警报信息。

 <script>
$(document).ready(function () {
    varifyForLogin();
});
function varifyForLogin() {
    if ('@ViewBag.Login' == 'No') {
        //Give your alert message here
        alert('Please answer to all the Question.');
    }
    document.getElementById('UserName').focus();
}

在view脚本中使用以下代码

<script>
$.post("Home/Index", function(result) {
<br/>
    &nbsp;&nbsp;&nbsp;&nbsp;//result will be your message
<br/>
});
</script>
<br/>

控制器:

public ActionResult Index(){
    //do some stuff
    string msg="your message";
    return Json(msg,JsonRequestBehaviour.AllowGet);
}

最新更新