0

I try to send antiforgerytoken and yeniTest is filled by some data ( it is not null ) to controller via below code at html page.

 var token = document.querySelector('input[name="__RequestVerificationToken"]').value;

    $.ajax({
        url: '@Url.Action("/TestCevabı")',
        type: 'post',
        data: { __RequestVerificationToken: token, yeniTest: JSON.stringify(yeniTest)},
        contentType: 'application/x-www-form-urlencoded; charset=utf-8',
        traditional: true
    });

Controller method is like below

    [HttpPost]
    [ValidateAntiForgeryToken]
    public bool TestCevabı(Test yeniTest)
    {
        ///
    }

I pass ValidateAntiForgeryToken at controller method.But yeniTest at TestCevabı method is null.

Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
Mehmet
  • 739
  • 1
  • 6
  • 17
  • 4
    Have you given `yeniTest` a value? That doesn't appear to be the case with the code you've shown here. – Jasen Mar 19 '19 at 02:17
  • yes yeniTest at html page filled some data – Mehmet Mar 19 '19 at 02:39
  • Have a look at this https://stackoverflow.com/questions/15317856/asp-net-mvc-posting-json. And if you still have problems show us your `Test` class and the value of `yeniTest`. – Jasen Mar 19 '19 at 02:51

1 Answers1

0

I changed the controller method as below.

[HttpPost]
[ValidateAntiForgeryToken]
public bool TestCevabı(string yeniTest)
{
    ///
}

and add following code to TestCevabıService

        Test çözülenTest = new Test();
        using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(testJsonFormat)))
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(çözülenTest.GetType());
            çözülenTest = ser.ReadObject(ms) as Test;
            ms.Close();
        }     

Now antforgerytoken pass and Test object filled

Mehmet
  • 739
  • 1
  • 6
  • 17