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

问题描述

我一直面临的一个问题在控制器中的一些code的执行后显示的消息框

I have been facing an issue to display "Message Box" after executing of some code in 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;
        }

如被看见上面的例子,如果结果是真还是假的,那么我想显示消息框,说明登录是suceess还是失败。

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>

如果登录是存在的,我想定位到/客户/ CollaborationPortal,否则我想显示消息Authroization失败。

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

在此先感谢

推荐答案

有没有办法做到这一点在MVC简单,只要在WinForms应用程序。

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

最简单的方法在您的案件网页上显示消息框来改变从的ActionResult这个动作JsonResult,并替换如果与

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

,并在网页上,你需要使用AJAX(即使用提交表单jQuery的$。员额)和对结果的回调函数检查:

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

更新
您发布的code似乎不错,但有一个问题。成功的功能:

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

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

此功能将始终执行重定向,不管返回什么控制器。你需要检查是否data.result(如果你返回JSON作为JSON(新{结果=结果});)是真实的,然后重定向,否则显示警报。所以,请尝试:

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

另一件事:

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

如果你想要这个工作,你需要从getRegData返回null文本框时,一个是空的。

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

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

08-21 02:16