如何显示“消息框"使用MVC3控制器 [英] How to display "Message box" using MVC3 controller

查看:45
本文介绍了如何显示“消息框"使用MVC3控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在控制器中执行一些代码后,我遇到了显示消息框"的问题

例如:-

public ActionResult IsLoginExsit(CustomerDO loginData){JsonResult jsonResult = new JsonResult();if (!string.IsNullOrEmpty(loginData.UserName) && !string.IsNullOrEmpty(loginData.Password)){bool 结果 = Businesss.Factory.BusinessFactory.GetRegistrations().IsLoginExist(loginData.UserName, loginData.Password);jsonResult.Data = 结果;}返回 jsonResult;}

如上例所示,如果结果为真或假,那么我想显示消息框,说明登录成功或失败.

<form class="formStyle" action="#" method="POST" id="frmAuthenticate"><div class="row"><input class="text row" type="text" value="" id="txtUserName"/>

<div class="row"><input class="text row" type="password" value="" id="txtPassword"/>

<div class="最后一行"><a class="link" href="">忘记密码?</a><a class="button3" href="/Registration/Registration" title="注册" >注册</a><input type="submit" class="button4" id="btnGo" value="Go"/>

</表单>

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

$("#btnGo").click(function (e) {var RegData = getRegData();if (RegData === null) {console.log("指定数据!");返回;}var json = JSON.stringify(RegData)$.ajax({url: '/注册/IsLoginExsit',类型:'POST',数据类型:'json',数据:json,contentType: '应用程序/json;字符集=utf-8',成功:功能(数据){if(data.result == true){location.href = "/Customer/CollaborationPortal";}别的{alert("登录失败");//管他呢}}});返回假;});函数 getRegData() {var UserName = $("#txtUserName").val();var Password = $("#txtPassword").val();return { "用户名": 用户名, "密码": 密码 };}

提前致谢

解决方案

在 MVC 中没有办法像在 winforms 应用程序中那样简单.

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

return Json(new {result = result});

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

$("表单输入[type=submit]").click(function(){var formData = $(this).closest("form").serialize();$.post("urltoyourcontrollerhere/IsLoginExsit", formData, function(data){if(data && data.result == true){ alert("登录存在!");}});});

更新您发布的代码似乎没问题,但存在一个问题.成功函数:

成功:函数(数据){location.href = "/Customer/CollaborationPortal";}

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

成功:函数(数据){if(data.result == true){location.href = "/Customer/CollaborationPortal";}别的{alert("登录失败");//管他呢}}

另一件事:

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

如果你想让它工作,当文本框之一为空时,你需要从 getRegData 返回 null.

I have been facing an issue to display "Message Box" after executing of some code in controller

for ex:-

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

as seen in above example, if result is true or false, then i would like to display message box stating that login is suceess or fail.

<!-- 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>

If login is exist i want to navigate to "/Customer/CollaborationPortal", else i would like to display message "Authroization fail".

$("#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 };
            }

Thanks in advance

解决方案

There is no way do that in MVC as simple as in winforms application.

Simplest way to display message box on web page in your case is to change this action from ActionResult to JsonResult, and replace your if with:

return Json(new {result = result});

and, in web page you need to use ajax (i.e. submit a form using jquery's $.post) and in callback function check for result:

$("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!");}
    });
});

UPDATE The code you posted seems OK, but there is one problem. The success function:

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

This function will always perform redirect, no matter what controller has returned. You need to check if data.result (if you returned your json as Json(new {result = result});) is true, and then redirect, else display alert. So, try:

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

Another thing:

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

If you want this to work, you need to return null from getRegData when one of textboxes is empty.

这篇关于如何显示“消息框"使用MVC3控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆