Saving JSON received through a controller to the database MVC

asp.net asp.net-mvc c# entity-framework-6 model-view-controller

Question

Hello, I just began using Visual Studio and had a lot of questions. Thanks to the Stack Overflow Community, I now feel more knowledgeable. I'm attempting to use Model Binding to POST JSON to the controller and store it to a database.

I may use the following ajax to POST the JSON to the controller:

$.ajax({
        headers: {
            "Content-Type": "application/json",
            "Accept": "application/json"
        },
        type: 'GET',
        url: 'https://api.github.com/search/repositories?q=repos+topic:' + $(this).attr('id') + '&sort=stars&order=desc&per_page=10',
        success: function (data) {

        **ajaxresult.push(data);**
        debugger;

        table.empty();
        table.append("<thead><tr><th>Avatar</th><th>Name</th><th>Score</th><th>URL</th><th>Updated at</th></tr></thead>");
        $.each(data.items, function (i, object) {

            var row = $('<tr>').addClass('table-primary');
            row.append('<td><img src=' + object.owner.avatar_url + 'height=50px width=50px/></td>');
            row.append('<td>' + object.name + '</td>' + '<td>' + object.score + '</td>' + '<td>' + object.url + '</td>' + '<td>' + object.updated_at + '</td>');
            table.append(row);

            **ajaxresult[i] = { "Avatar": object.owner.avatar_url, "Name": object.name, "Score": object.score, "URL": object.url, "Updatedat": object.updated_at };**
        });
        **var myJSON = JSON.stringify(ajaxresult);**
        table.append('</table>');
        $('table').replaceWith(table);
        debugger;
        console.log(myJSON);
         $.ajax({

                    contentType: 'application/json; charset=utf-8',
                    datatype:'JSON',
                    type: 'POST',
                    url: 'http://localhost:60294/Git/Updateto',
                    **data: myJSON,**
             success: function (data) {
                 alert('Post Succesful');
                    },
                    error: function (data) {
                        alert('error');
                            }
                 });
    }

});

});

My controller and model are as follows:

[HttpPost]
        public async Task<IActionResult> Updateto(GitJSON gitjson)
    {
        if (ModelState.IsValid)
        {
            gitjson.gitdList[0].AvatarURL=;


        }
        await _context.SaveChangesAsync();

        return Ok();
    }

Model:

public class gitd
    {
        public int ID { get; set; }
        public string AvatarURL { get; set; }
        public string Name { get; set; }
        public decimal Score { get; set; }
        public DateTime Updatedat { get; set; }


    }
public class GitJSON
{
    public List<gitd> gitdList { set; get; }
}

According to what I understand, the controller binds the GitJSON model. The JSON pairs are thus automatically mapped to the public elements of the gitd model.

After doing some study, I discovered that thepublic List<gitd> gitdList { set; get; }

corresponds to a list of indexed items. Consequently, I believe thatgitjson.gitdList[0].AvatarURL=; is now referring to the initial JSON object I passed's AvatarURL attribute. Is what I've understood correct? How do I now save the JSON to the database if that is the case?

Also, if I addedreturn View(); I get a 500 error in the Updateto controller. The Updateto controller does not yet have a view added. Could this be the only explanation? If true, shouldn't the error be a Not found one rather than a 500 one?

Although I added a mock view to the Updateto controller,return view(); results in a 500 error.

1
1
7/19/2018 3:05:01 PM

Accepted Answer

ZZZ_tmp
1
7/19/2018 4:53:31 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow