Контроллер MVC asp.net не может проанализировать JSON - PullRequest
0 голосов
/ 18 ноября 2018

У меня есть одна проблема, мне нужно отправить данные (json) с клиента на сервер (asp.net mvc), я отправляю данные (использую обещание), данные отправляются, и контроллер видит данные, ноон видит только элементы, каждый элемент моих данных нулевой, может ли что-нибудь помочь мне?

Мой контроллер:

    [HttpPost]
    public ActionResult Receiver(IEnumerable<MyViewModel> model)
    {
        var hh = model;

        return Json(hh);
    }

данные:

public class MyViewModel
{
    public string Text { get; set; }
    public DateTime Date { get; set; }
}

код клиента:

<input type="button" id="btn" />
<input type="text" id="text" />
<input type="date" id="date" />

<div id="div"></div>

<script>
    const btn = document.getElementById('btn');
    const div = document.getElementById('div');
    const text = document.getElementById('text');
    const date = document.getElementById('date');

    function httpGet(url) {

        return new Promise(function (resolve, reject) {

            var xhr = new XMLHttpRequest();
            xhr.open('POST', url, true);

            xhr.setRequestHeader('Content-Type', 'application/json');

            xhr.onload = function () {
                if (this.status == 200) {
                    resolve(this.response);
                } else {
                    var error = new Error(this.statusText);
                    error.code = this.status;
                    reject(error);
                }
            };

            xhr.onerror = function () {
                reject(new Error("Network Error"));
            };

            const t = text.value;
            const d = date.value;

            var value = `{ "Text": '${t}', "Date": '${d}'}`;
            var value2 = `{ "Text": '${t}', "Date": '${d}'}`;

            var things = [];
            things.push(value);
            things.push(value2);

            var s = JSON.stringify(things);

            xhr.send(s);
        });    
    }

    btn.addEventListener('click', () => {    
        httpGet('@Url.Action("Receiver", "Home")')
            .then(
            response => { 
                const result = JSON.parse(response, dateTimeReviver);
                div.innerHTML = result.Text +" "+ result.Date; 
            },
                error => alert(`Rejected: ${error}`)
        );
    });
</script>

Спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...