与Json结果进行比较

本文关键字:比较 结果 Json | 更新日期: 2023-09-27 17:58:36

我有返回JSON 的操作

[HttpPost]
public JsonResult Validate(string arg1, string arg2)
{
    bool check
    ...
    return Json(!check ? new { message = "-1" } : new { message = "1" }, JsonRequestBehavior.AllowGet);
}

我需要从另一个行动中调用该行动。在另一个后期操作中,我需要对客户端调用(使用ajax)和服务器端验证进行比较。

我怎样才能从那个动作中得到信息?

var a = Validate(model.arg1, model.arg2);
a.Data;

返回json。我如何从中获得价值来进行比较?

与Json结果进行比较

  1. 创建一个新的类文件。假设您将其命名为helper.cs

    命名空间YourProject.Helper{公共类帮助程序{//此处显示您的验证代码。}}

  2. 通过using YourProjectNS.Helper 在控制器中导入此类

    使用MyProject.Helper;

  3. helper类中定义Validate函数。

    公共类帮助程序{public bool Validate(字符串arg1,字符串arg2){bool检查。。。return Json(!check?new{message="-1"}:new{message="1"});}}

  4. 在您的控制器中,无论您在哪里需要它,都可以将其作为helperObj.Validate(v,v2) 访问

    Helper helperObj=新Helper();bool isValid=helperObj.Validate(v1,v2);

最终,您的代码将看起来像:

在Helper.cs:中

namespace MyProject.Helper{
    public class Helper{
        public bool Validate(string arg1, string arg2)
        {
            bool check
            ...
            return Json(!check ? new { message = "-1" } : new { message = "1" });
        }
    }
}

在控制器操作中:

using MyProject.Helper;
public ActionResult MyAction(){
    ...
    // your other code
    Helper helperObj = new Helper();
    bool isValid = helperObj.Validate(v1, v2);
    ...
}

希望这能有所帮助。