jQuery 如何使用MVC3控制器显示“消息框”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7553575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display "Message box" using MVC3 controller
提问by Pardha
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".
如果存在登录,我想导航到“/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 };
}
Thanks in advance
提前致谢
回答by Goran Obradovic
There is no way do that in MVC as simple as in winforms application.
在 MVC 中没有办法像在 winforms 应用程序中那样简单。
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:
在您的案例中,在网页上显示消息框的最简单方法是将此操作从 ActionResult 更改为 JsonResult,并将您的 if 替换为:
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:
并且,在网页中,您需要使用 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!");}
});
});
UPDATEThe 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:
无论控制器返回什么,此函数将始终执行重定向。您需要检查 data.result (如果您将 json 作为 Json(new {result = result}); 返回)为真,然后重定向,否则显示警报。所以,试试:
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.
如果你想让它工作,当文本框之一为空时,你需要从 getRegData 返回 null 。
回答by jgauffin
You CAN display a message box when done.
完成后您可以显示一个消息框。
public ActionResult LoginExist(BAMasterCustomerDO loginData)
{
return new JavascriptResult { Script = "alert('Saved successfully');" };
}
回答by ipr101
You won't be able to display a message box using the server side code. You'll need to pass some data back to the view indicating the login status then write some client side code to display the message box.
您将无法使用服务器端代码显示消息框。您需要将一些数据传递回表示登录状态的视图,然后编写一些客户端代码来显示消息框。
回答by Darin Dimitrov
You could use a property of your view model which will be passed to the view and which will contain this information:
您可以使用视图模型的属性,该属性将传递给视图并包含以下信息:
var model = new MyViewModel();
bool result = Businesss.Factory.BusinessFactory.GetRegistrations().IsLoginExist(loginData.UserName, loginData.Password);
model.IsLoginSuccess = result;
...
return View(model);
and inside your strongly typed view you would test the value of this property and display the message accordingly:
在强类型视图中,您将测试此属性的值并相应地显示消息:
@if (Model.IsLoginSuccess)
{
<div>The Login was successful</div>
}