Answer the question
In order to leave comments, you need to log in
json stats method not executing?
Please help me figure it out, I don’t ask me to do it, I ask you to figure it out, understand the problem and solve it. There is an MVC project, a game project, here is the code of the HomeController file
using Newtonsoft.Json;
using System.Web.Mvc;
using WebApplication.Models;
using XO;
namespace WebApplication.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new StatisticsModels());
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
[HttpPost]
public bool SaveGameInfo([System.Web.Http.FromBody]dynamic stat)
{
var newStat = JsonConvert.DeserializeObject<Statistcs>(stat.StatisticsList);
return StatisticsModels.AddStatistics(newStat);
}
[HttpGet]
public string GetGameInfo()
{
return JsonConvert.SerializeObject(new StatisticsModels().StatisticsList);
}
}
}
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using XO;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication.Models
{
public class StatisticsModels
{
public List<Statistcs> StatisticsList { get; set; }
public StatisticsModels()
{
var jsonData = File.ReadAllText(@"C:\Users\EvgenieL\Source\Repos\XO\XO\XO\bin\Debug\stats.json");
StatisticsList = JsonConvert.DeserializeObject<List<Statistcs>>(jsonData);
}
public static bool AddStatistics(Statistcs stat)
{
//var conn = new SqlConnection(@"Data Source=localhost;Initial Catalog=XO;Integrated Security=true");
//var command = new SqlCommand();
//command.Connection = conn;
//command.CommandType = CommandType.Text;
//command.CommandText = $"INSERT INTO Statistic(Date, Result, StepCounter, UserFirst) VALUES('{stat.Date}', '{stat.Result}', {stat.StepCounter}, '{stat.UserFirst}')";
//try
//{
// conn.Open();
// command.ExecuteNonQuery();
//}
//catch
//{
// return false;
//}
//finally
//{
// conn.Close();
//}
//return true;
try
{
var filePath = "stats.json";
var jsonData = File.ReadAllText(filePath);
var statisticsList = JsonConvert.DeserializeObject<List<Statistcs>>(jsonData)
?? new List<Statistcs>();
statisticsList.Add(stat);
jsonData = JsonConvert.SerializeObject(statisticsList);
File.WriteAllText(filePath, jsonData);
}
catch
{
return false;
}
return true;
}
}
}
Answer the question
In order to leave comments, you need to log in
AddStatistics method fails- What means? Not called? Passes through it and there is no result? Is an error thrown? Does it come out somewhere in the middle?
catch
{
return false;
}
var jsonData = File.ReadAllText(@"C:\Users\EvgenieL\Source\Repos\XO\XO\XO\bin\Debug\stats.json");-Very bad idea. Just unrealistically bad hardcode.
var filePath = "stats.json";
var jsonData = File.ReadAllText(filePath);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question