简体   繁体   English

空JSON GET ASP.NET MVC

[英]Null JSON GET ASP.NET MVC

Hi I have two controller methods. 嗨,我有两种控制器方法。 I am passing two parameters from the 1st method to the 2nd. 我正在将两个参数从第一种方法传递给第二种方法。 The values inserted to database are correct and not NULL. 插入数据库的值是正确的,而不是NULL。 However when there are displayed back on the webpage in the return Json line, they come out as null and im not sure as to why? 但是,当返回的Json行显示在网页上时,它们显示为null,并且不确定为什么? Here are the controller methods: 这是控制器方法:

  [HttpPost]
    public void CalculateAndSaveToDB(BMICalculation CalculateModel)
    {
        if (ModelState.IsValid)
        {
            CalculateModel.Id = User.Identity.GetUserId();
            CalculateModel.Date = System.DateTime.Now;
            CalculateModel.BMICalc = CalculateModel.CalculateMyBMI(CalculateModel.Weight, CalculateModel.Height);
            CalculateModel.BMIMeaning = CalculateModel.BMIInfo(CalculateModel.BMICalc);
            db.BMICalculations.Add(CalculateModel);
            db.SaveChanges();
        }

        CalculateAndSaveToDB(CalculateModel.BMICalc.ToString(), CalculateModel.BMIMeaning.ToString());
    }



    public JsonResult CalculateAndSaveToDB(string o, string t)
    {
        var data = new
        {
            CalculatedBMI = o,
            CalculatedBMIMeaning = t
        };

        return Json(data, JsonRequestBehavior.AllowGet);
    }

Update 更新资料

BMICalculationsModel: BMICalculationsModel:

 public partial class BMICalculation
{
    public string Id { get; set; }
    public System.DateTime Date { get; set; }
    public Nullable<double> BMICalc { get; set; }

    [Required]
    public double Height { get; set; }

    [Required]
    public double Weight { get; set; }
    public int BMICalculationID { get; set; }
    public virtual AspNetUser AspNetUser { get; set; }
    public string BMIMeaning { get; set; }

    public double CalculateMyBMI(double KG, double Height)
    {
        return KG / (Height * Height);
    }

    public string BMIInfo(double? BMI)
    {
        string BMIInfo = "";

        if (BMI <= 18.5)
        {
            BMIInfo = "Underweight";

        }
        else if (BMI > 18.5 && BMI < 25)
        {
            BMIInfo = "Average";
        }

        else if (BMI > 25)
        {
            BMIInfo = "Overweight";
        }

        return BMIInfo;
    }

}

You need to make your first method return JsonResult and not void . 您需要使第一个方法返回JsonResult而不是void The second CalculateAndSaveToDB returns a JsonResult which never gets used. 第二个CalculateAndSaveToDB返回一个JsonResult,它永远不会被使用。

I would definitely not call that second method CalculateAndSaveToDB as it doesn't save anything to the DB. 我绝对不会调用第二种方法CalculateAndSaveToDB因为它不会将任何内容保存到数据库中。 Maybe GenerateJsonCalc would be more suitable or maybe no method at all: 也许GenerateJsonCalc更合适,或者根本没有方法:

[HttpPost]
public JsonResult CalculateAndSaveToDB(BMICalculation CalculateModel)
 {
      if (ModelState.IsValid)
      {
          CalculateModel.Id = User.Identity.GetUserId();
          CalculateModel.Date = System.DateTime.Now;
          CalculateModel.BMICalc = CalculateModel.CalculateMyBMI(CalculateModel.Weight, CalculateModel.Height);
          CalculateModel.BMIMeaning = CalculateModel.BMIInfo(CalculateModel.BMICalc);
          db.BMICalculations.Add(CalculateModel);
          db.SaveChanges();
    }

    return CalculateAndSaveToDB(CalculateModel.BMICalc.ToString(), CalculateModel.BMIMeaning.ToString());

I would go for something like: 我会喜欢这样的:

return Json(new
{
    CalculatedBMI = CalculateModel.BMICalc.ToString(),
    CalculatedBMIMeaning = CalculateModel.BMIMeaning.ToString()
 }, JsonRequestBehavior.AllowGet);

Change your return type of your POST method and its last line to this: 将您的POST方法的返回类型及其最后一行更改为:

public ActionResult CalculateAndSaveToDB(BMICalculation CalculateModel)
{
    //stuff

    return RedirectToAction("CalculateAndSaveToDB", new { o = CalculateModel.BMICalc.ToString(), t = CalculateModel.BMIMeaning.ToString());
}

try return a dynamic object 尝试返回动态对象

public  dynamic  CalculateAndSaveToDB(BMICalculation CalculateModel)
{
   ...

   return  CalculateAndSaveToDB(CalculateModel.BMICalc.ToString(), CalculateModel.BMIMeaning.ToString());
}  

public dynamic CalculateAndSaveToDB(string o, string t)
{
    dynamic data = new new ExpandoObject();

    data.CalculatedBMI = o;
    data.CalculatedBMIMeaning = t;


    return  data ;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM