0

I'm working on a ASP.NET MVC project and I have a controller where I am getting JSON data from an external Web API. Now I want to deserialize this JSON data and put it into a model that will eventually be passed into a Partial View.

JSON Data:

{
"results":[
{
    "name":"Company A",
    "providerName":"Company Provider A"
...(More Data Below)

Controller:

[HttpGet]
[Route("company-info/companyinfogetapidata")]
[AllowAnonymous]
public ActionResult CompanyInfoGetApiData(string name, int CompanyCode, string city, string state, int zip)
    {

        /* Json Data is fetched properly */

        var json = request.Result.Content.ReadAsStringAsync().Result; 

        JObject o = JObject.Parse(json);

        JToken ApiData = o["results"];

       // Now I want to pass the JSON data into my model

        CompanyResultsModel getfetcheddata = new CompanyResultsModel();

       // I tried using this method below but it's not working properly

        getfetcheddata = Newtonsoft.Json.JsonConvert.DeserializeObject<CompanyResultsModel>(json);

       // Pass the Model containing into the PartialView Result
        return PartialView(@"~/Views/Shared/companies/_companyResults.cshtml", getfetcheddata);

    }

Model:

public class CompanyResultsModel
{
    public string companyName { get; set; }
    public string companyProvider { get; set; }
}

View:

@model Companies.CompanyResultsModel

<p>@Model.companyName</p>
<p>@Model.companyProvider</p>

After I pass in the JSON data into my model, if I use this method above for displaying the data in my view, will it return all of my data properly?

Any help is greatly appreciated!

6
  • What is not working? What errors are you getting? And showing the actual data your receiving would help. Commented Oct 18, 2017 at 1:38
  • @StephenMuecke The problem is none of the data is actually getting passed properly to the model. When the partial view is returned, none of the JSON data is actually being displayed. Commented Oct 18, 2017 at 1:47
  • maybe the CompanyResultsModel's properties do not match the json struct Commented Oct 18, 2017 at 1:49
  • @aspark I made an edit above to show what the JSON data looks like. Commented Oct 18, 2017 at 1:51
  • Your data is not correct despite what you claim - you receiving a collection, not a single object. It would need to look like (for testing) string json = @"{ 'companyName ': 'abc', 'companyProvider': 'xyz' }"; (or you need to deserialize to a model containing a property results which is IEnumerable<CompanyResultsModel> Commented Oct 18, 2017 at 1:52

1 Answer 1

1

The json your receiving does not match your CompanyResultsModel. It does match a model that contains a property named results which is a collection of CompanyResultsModel.

Create the following model

public class JsonResultModel
{
    public IEnumerable<CompanyResultsModel> Results { get; set; }
}

and in the controller

JsonResultModel model = JsonConvert.DeserializeObject<JsonResultModel>(json);

and if you want to return only the first CompanyResultsModel to the view

return PartialView("_companyResults", model.Results.FirstOrDefault());
Sign up to request clarification or add additional context in comments.

4 Comments

@CodingYoshi - No I meant just what I said - OP is trying to serialize that json to CompanyResultsModel which does not match. He needs another model that does match (which is what I have shown - the JsonResultModel)
@StephenMuecke So I just tested this code and did the following: 1) Created the JsonResultsModel, 2) Added the JsonResultModel to the controller, 3) Returned it with return PartialView("_companyResults", model);, 4) In my View, added @Model Models.LoanJsonData, <p>@Model.Results</p>. In my view, I get the following output: System.Collections.Generic.List1[Companies.LoanJsonModel]
That is not what my answer states! And what is LoanJsonData? If that is the JsonResultsModel I referred to, and your returning that to the view, then @Model.Results is a collection of CompanyResultsModel so you would need to iterate through it - e.g. @foreach { var item in Model.Results) { <p>@item.companyName</p> .... }
@StephenMuecke I modified your solution a little bit but it lead me in the right direction of passing JSON data into my View. I'm going to edit my question above soon. Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.