-3

I have Api Controller

public class JsonController : Controller
{
    private readonly IRepositor _repositor;
    public JsonController(IRepositor repositor)
    {
        _repositor = repositor;
    }
    // GET: api/Json
    [HttpGet]
    public string Get()
    {
        return Repositions.StrocTable;
    }
    // POST: api/Json
    [HttpPost]
    public void Post([FromBody]List<Json> jsons)
    {
        _repositor.DataSetTwo(jsons);
    }
}

and class

public class Repositions : IRepositor
{
    public static string StrocTable { get; set; }
    public void DataSetTwo(List<Json> cheked)
    {
        for (int i = 0; i < cheked.Count && cheked[i].Name == "Check"; i++)
            StrocTable += cheked[i].Value;
    }
}

so I wanna execute this script

$.ajax({
    type: 'POST',
    url: '/api/Json',
    data: JSON.stringify(jsons),
    contentType: 'application/json'
}).done($(hash).empty();

$.get('/api/Json').done(function (Stroctable) {
    $('#Tables').append(Stroctable);
}););

i was got empty string after when i execute second times I've get string that should have come the first time. And I don't know how call this question. Thanks.

Mustapha Larhrouch
  • 3,373
  • 3
  • 14
  • 28
Chaffee
  • 43
  • 8
  • 1
    You should take a look at the lifecycle of controllers for web apis (https://stackoverflow.com/a/1763908/8336973). In short: There is a new controller for each request. Due to that `Repositions.StrocTable` is not set when you are executing the `Get` request. – RedFox Nov 08 '18 at 08:19
  • By using a `static` property (StrocTable), every user will see the same value. This may or may *not* be what you want. – Hans Kesting Nov 08 '18 at 08:50
  • Thanks for answers – Chaffee Nov 08 '18 at 09:03

1 Answers1

0

You're going to want to return Json from your controller then deal with it on your front end JavaScript.

Change your Get method to:

[HttpGet]
public string Get()
{
    return Json(Repositions.StrocTable);
}
Alex
  • 66
  • 3