jquery - 如何使用MVC3 Controller 显示 "Message box"

标签 jquery visual-studio asp.net-mvc-3 c#-3.0

在 Controller 中执行某些代码后,我遇到了显示“消息框”的问题

例如:-

public ActionResult IsLoginExsit(CustomerDO loginData)
        {

            JsonResult jsonResult = new JsonResult();            
             if (!string.IsNullOrEmpty(loginData.UserName) && !string.IsNullOrEmpty(loginData.Password))                 
             {                     
                 bool result = Businesss.Factory.BusinessFactory.GetRegistrations().IsLoginExist(loginData.UserName, loginData.Password);                     

                     jsonResult.Data = result;

             }      
             return jsonResult;
        }

如上面的示例所示,如果结果为 true 或 false,那么我想显示消息框,说明登录成功或失败。

<!-- language: lang-.html -->
    <form class="formStyle" action="#" method="POST" id="frmAuthenticate">
            <div class="row">
            <input class="text row" type="text" value="" id="txtUserName"/>
            </div>
            <div class="row">
            <input class="text row" type="password" value="" id="txtPassword" /> 
            </div>  
            <div class="row last">
            <a class="link" href="">Forgot Password?</a>   
            <a class="button3" href="/Registration/Registration" title="Registration" >Signup</a>        
<input type="submit" class="button4" id="btnGo" value="Go" />            
            </div>
        </form>

如果登录存在,我想导航到“/Customer/CollaborationPortal”,否则我想显示消息“授权失败”。

$("#btnGo").click(function (e) {
                var RegData = getRegData();
                if (RegData === null) {
                    console.log("Specify Data!");
                    return;
                }
 var json = JSON.stringify(RegData)
                $.ajax({
                    url: '/Registration/IsLoginExsit',
                    type: 'POST',
                    dataType: 'json',
                    data: json,
                    contentType: 'application/json; charset=utf-8',
                    success: function (data) {                     
                    if(data.result == true){                                                 
                    location.href = "/Customer/CollaborationPortal";                     
                    }                    
                     else{
                         alert("Login failed"); //or whatever                     
                     }             
                     }
                });
return false;
            });
            function getRegData() {

                var UserName = $("#txtUserName").val();
                var Password = $("#txtPassword").val();
                return { "UserName": UserName, "Password": Password };
            }

提前致谢

最佳答案

在 MVC 中不可能像在 winforms 应用程序中那样简单。

在您的情况下,在网页上显示消息框的最简单方法是将此操作从 ActionResult 更改为 JsonResult,并将 if 替换为:

return Json(new {result = result});

并且,在网页中您需要使用ajax(即使用jquery的$.post提交表单)并在回调函数中检查结果:

$("form input[type=submit]").click(function(){
    var formData = $(this).closest("form").serialize();
    $.post("urltoyourcontrollerhere/IsLoginExsit", formData, function(data){
        if(data && data.result == true){ alert("Login exists!");}
    });
});

更新 您发布的代码看起来没问题,但有一个问题。成功函数:

success: function (data) {
                        location.href = "/Customer/CollaborationPortal";
                    }

无论返回哪个 Controller ,该函数都将始终执行重定向。您需要检查 data.result (如果您将 json 返回为 Json(new {result = result});)是否为 true,然后重定向,否则显示警报。所以,尝试:

success: function (data) {
                    if(data.result == true){                        
                        location.href = "/Customer/CollaborationPortal";
                    }
                    else{
                        alert("Login failed"); //or whatever
                    }
            }

另一件事:

            var RegData = getRegData();
            if (RegData === null)

如果您希望此方法起作用,则需要在其中一个文本框为空时从 getRegData 返回 null。

关于jquery - 如何使用MVC3 Controller 显示 "Message box",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7553575/

相关文章:

jquery - 绑定(bind)到已经发生的事件并自动运行处理程序

visual-studio - Visual Studio : Re-enable "Build failed, run last success?" dialogue box

html - 什么替代了 HTML5 表格中的 cellpadding、cellspacing、valign 和 align?

c# - DataAnnotation 和可选的 DateTime 值

entity-framework-4 - 工作单元应该指向服务层还是存储库?

c# - 使用 Enterprise Library 5.0 数据访问问题

javascript - jQuery 从具有 AngularJS 内容的页面加载 #div

Javascript split() 弄乱了我的 html 标签

javascript - 使用逗号分隔值巧妙地获取查询字符串

C++ VS更改项目目录后需要重新编译