简体   繁体   English

发送消息的最佳做法是什么

[英]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.

相关问题 嵌套函数调用 - 什么是最佳实践? - Nested function calls - What's the best practice? 显示条件确认消息然后继续的最佳做法是什么? - What is the best practice for show conditional confirmation message then continue? 针对第三方dll进行编程的最佳实践是什么? - What's the best practice for programming against a 3rd party dll? 在MVC中以简单的层次结构构建模型,最佳实践是什么? - Structuring models in a simple hierarchy in MVC, what's the best practice? 触发手动OnClick事件的最佳做法是什么? - What's the Best Practice for Firing Manual OnClick Events? 实体框架中多个“包含”的最佳实践是什么? - What is the best practice for multiple “Include”-s in Entity Framework? 在 C# 中表示 Time 对象的最佳实践是什么? - What's best practice to represent a Time object in C#? 在Unity的C#中使用静态变量的最佳实践是什么? - What's the best practice to using the static variable in C# of Unity 在实例化时将大量信息转移到另一个类的最佳实践是什么? - What's the best practice to transfer much information to another class at instantiation? 使类的属性线程安全的最佳实践是什么? - What is a best practice for making a class's properties thread safe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM