ASPCoder1450
ASPCoder1450

Reputation: 1651

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. 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? 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:

 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;
    }

}

Upvotes: 0

Views: 125

Answers (3)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149538

You need to make your first method return JsonResult and not void. The second CalculateAndSaveToDB returns a JsonResult which never gets used.

I would definitely not call that second method CalculateAndSaveToDB as it doesn't save anything to the DB. Maybe GenerateJsonCalc would be more suitable or maybe no method at all:

[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);

Upvotes: 3

JB06
JB06

Reputation: 1931

Change your return type of your POST method and its last line to this:

public ActionResult CalculateAndSaveToDB(BMICalculation CalculateModel)
{
    //stuff

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

Upvotes: 0

Math
Math

Reputation: 818

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 ;
}

Upvotes: -1

Related Questions