最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unable to cast object of type 'System.Text.Json.JsonElement' to type 'System.IConvertible&a

programmeradmin2浏览0评论

After converting my application into .NET Core 3.1 , the FromBody json attribute is not working . So Ichanged into [FromBody] JsonElement model. After changing that how can I get the value from model into variable.

In Javascript

 var model = {
                Employees: EmpIds,
                FromDate: $('#fromDate').val(),
                ToDate: $('#toDate').val(),
                Comment:  $('#ment').val(),
            }
            var url = "/Attendance/BulkUpdate"
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify(model),
                success: function (response) {
                    if (response.success) {
                        ShowResultModalPopup(response.responseText);
                    } else {
                        // DoSomethingElse()
                        ShowErrorModalPopup(response.responseText);
                    }
                },
                failure: function (response) {
                    console.log(response.responseText);
                },
                error: function (response) {
                    console.log(response.responseText);
                }
            });
    
    
        }

In Controller

public IActionResult BulkUpdate([FromBody] JsonElement model)
{
            DateTime fromdate = Convert.ToDateTime(model.GetProperty("FromDate"));// How can I store the value from model variable into datetime
            DateTime todate = Convert.ToDateTime(model.GetProperty("ToDate"));
}

Thanks Pol

After converting my application into .NET Core 3.1 , the FromBody json attribute is not working . So Ichanged into [FromBody] JsonElement model. After changing that how can I get the value from model into variable.

In Javascript

 var model = {
                Employees: EmpIds,
                FromDate: $('#fromDate').val(),
                ToDate: $('#toDate').val(),
                Comment:  $('#ment').val(),
            }
            var url = "/Attendance/BulkUpdate"
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify(model),
                success: function (response) {
                    if (response.success) {
                        ShowResultModalPopup(response.responseText);
                    } else {
                        // DoSomethingElse()
                        ShowErrorModalPopup(response.responseText);
                    }
                },
                failure: function (response) {
                    console.log(response.responseText);
                },
                error: function (response) {
                    console.log(response.responseText);
                }
            });
    
    
        }

In Controller

public IActionResult BulkUpdate([FromBody] JsonElement model)
{
            DateTime fromdate = Convert.ToDateTime(model.GetProperty("FromDate"));// How can I store the value from model variable into datetime
            DateTime todate = Convert.ToDateTime(model.GetProperty("ToDate"));
}

Thanks Pol

Share Improve this question edited Apr 21, 2022 at 20:39 Ray Hayes 15k8 gold badges56 silver badges78 bronze badges asked Jun 11, 2021 at 11:19 Alan PauilAlan Pauil 2091 gold badge6 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You could change like below:

DateTime fromdate = Convert.ToDateTime(model.GetProperty("FromDate").GetString());
DateTime todate = Convert.ToDateTime(model.GetProperty("ToDate").GetString());

You can use the built in methods to convert those properties to dates:

public IActionResult BulkUpdate([FromBody] JsonElement model)
{
    var fromdate = model.GetProperty("FromDate").GetDateTime();
    var todate = model.GetProperty("ToDate").GetDateTime();
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论