Как я могу отправить на json данные для моего контроллера в ajax mvc? - PullRequest
0 голосов
/ 19 января 2020

Моя проблема: я отправляю JSON данные, но мои JSON данные обнуляются? Я не понимаю, почему он обнуляется.

Я прочитал всю документацию, но я новичок в jquery. Что мне не хватает? Мне нужно отправить JSON данные для моего контроллера.

Index.cs html:

var UserData = {
    "CustomerID": "99999",
    "CustomerCode": "0",
    "UserID":"127",
    "StartDate": "02/09/2019",
    "FinishDate": "03/08/2020",
    "SuccesID": "1",
    "Cash": "1",
    "ProblemID": "1",
    "PageNo":"1",
    "DataNo":"1"
};
var userDataJson = JSON.stringify(UserData);
$.ajax({
    type: "POST",
    url: "/Problems/Search",
    contentType: "application/json",
    data: userDataJson,
    success: function (msg) {
        console.log(msg);
    },
    error: function (err) {
        alert("search error");
    }
});

Модель:

public class ProblemsInput
{
    public string CustomerID { get; set; }
    public string CustomerCode { get; set; }
    public string UserID { get; set; }
    public string StartDate { get; set; }
    public string FinishDate { get; set; }
    public string SuccesID { get; set; }
    public string Cash { get; set; }
    public string ProblemID { get; set; }
    public string PageNo { get; set; }
    public string DataNo { get; set; }
}

Контроллер:

[HttpPost]
public IActionResult Search(ProblemsInput problemsinput)
{
    return View();
}

Ответы [ 3 ]

0 голосов
/ 20 января 2020

Чувак. Эта проблема - ваши UserData. Попробуйте этот код:

var problemsinput= {
    CustomerID: '99999',
    CustomerCode: '0',
    UserID:'127',
    StartDate: '02/09/2019',
    FinishDate: '03/08/2020',
    SuccesID: '1',
    Cash: '1',
    ProblemID: '1',
    PageNo: '1',
    DataNo: '1',
};

 $.ajax({
     type: "POST",
     url: "/Problems/Search",
     contentType: "application/json",
     data: {problemsinput},
     success: function (msg) {
         console.log(msg);
     },
     error: function (err) {
         alert("search error");
     }
});

Если это не работает. Используйте атрибут [FromBody] в Controller и попробуйте снова. Я надеюсь, что это поможет вам.

0 голосов
/ 20 января 2020

Вам просто нужно добавить

 data: JSON.stringify({"problemsinput":userDataJson}),

вместо data: userDataJson, Это решит проблему.

Ajax вызов будет

var userDataJson = JSON.stringify(UserData);
     $.ajax({
         type: "POST",
         url: "/Problems/Search",
         contentType: "application/json",
         data: JSON.stringify({"problemsinput":userDataJson}),
         success: function (msg) {
             console.log(msg);
        },
        error: function (err) {
            alert("search error");
        }
    });
0 голосов
/ 19 января 2020

Просто примените атрибут FromBody к параметру problemsinput:

[HttpPost]
public IActionResult Search([FromBody]ProblemsInput problemsinput) 
{ 
    return View(); 
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...