[英]What's the best practice to send a message
The objective: 目标:
Call an action and show a message for display the result "success/error", 调用一个动作并显示一条消息以显示结果“成功/错误”,
The Problem: 问题:
Actually, I know how to do this using JQuery, but I don't know if that's the best way, 实际上,我知道如何使用JQuery做到这一点,但我不知道这是否是最好的方法,
?? ?? I would like to know if it could be better doing all of these from server side, I mean call only an ActionLink and then execute a javascript or return a message from server.
我想知道是否最好从服务器端进行所有这些操作,我的意思是仅调用ActionLink,然后执行JavaScript或从服务器返回消息。
The Code: MVC3 C# 代码: MVC3 C#
View: 视图:
$('#btnValidate')
.click(function(e) {
$.ajax({
url: "/Valoration/ValidateHvm" ,
type: "POST",
success: function(data, status, xhr) {
if(data.success){
alert('success');
}else{
alert('error');
},
error: function(xhr, status, err) {
alert('error');
}
});
});
Controller: 控制器:
[HttpPost]
public ActionResult ValidateHvm()
{
var result = HvmService.ValidateProject;
return Json(new { success = result});
}
There is nothing wrong with your implementation, it seems the best way to me, but another way is you can use ModelState.AddModelError() if you wish but your actionresult will need to redisplay the view with the model 您的实现没有错,这对我来说似乎是最好的方法,但是另一种方法是,如果您愿意,可以使用ModelState.AddModelError(),但是您的操作结果将需要使用模型重新显示视图
So you post will accept the model 所以你发布会接受模型
[HttpPost]
public ActionResult ValidateHvm(ValidateModel model)
{
var result = HvmService.ValidateProject(model);
if(result == false){ //or whatever
ModelState.AddModelError("","Error");
}
return View("MyView", model)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.