1

I am trying to read a .json File and return the value as a table in asp .net Core 3.1! I am new at working with asp .net core, so please help me from a beginners perspective.

Consider the code snippet below. My problem is that I cannot pass the values from the controller to the view.

I encounter this exception:

An unhandled exception occurred while processing the request.
InvalidOperationException: The model item passed into the ViewDataDictionary is of type '.Sample.Models.Mstype', but this ViewDataDictionary instance requires a model item of type 'Sample.Models.Mstypes'.

Controller

  public IActionResult Run()
        {
            ...

            var webClient = new WebClient();
            var json = webClient.DownloadString(@"/app/results/type.json");
            var mstypes = JsonConvert.DeserializeObject<Mstype>(json);
            
            return View(mstypes);

        }

Mstype.cs

  public int CIS { get; set; }
        public int PPMS { get; set; }
        public int RRMS { get; set; }
        public int SPMS { get; set; }
        public int not_sure { get; set; }

Mstypes.cs

 public class Mstypes
    {
        public IList<Mstype> mstypes { get; set; }
    }

View

@model Fed_PiP.Models.Mstypes

@{
    ViewData["Title"] = "Run";
}


<table class="table">

    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(Model => Model.mstypes[0].CIS)
            </th>
            <th>
                @Html.DisplayNameFor(Model => Model.mstypes[0].PPMS)

            </th>
            <th>
                @Html.DisplayNameFor(Model => Model.mstypes[0].RRMS)

            </th>
            <th>
                @Html.DisplayNameFor(Model => Model.mstypes[0].SPMS)

            </th>
            <th>
                @Html.DisplayNameFor(Model => Model.mstypes[0].not_sure)

            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>

       
            <tr>
                <td>
                    @Model.mstypes.FirstOrDefault().CIS
                </td>
                <td>
                    @Model.mstypes.FirstOrDefault().PPMS
                </td>
                <td>
                    @Model.mstypes.FirstOrDefault().RRMS
                </td>
                <td>
                    @Model.mstypes.FirstOrDefault().SPMS
                </td>
                <td>
                    @Model.mstypes.FirstOrDefault().not_sure
                </td>
            </tr>

    </tbody>
</table>

and finally json file

{"CIS":11,"PPMS":12,"RRMS":6,"SPMS":11,"not_sure":10}

also, I have a question is there any way to make a dynamic table from the static JSON files? I mean without defining the model or ViewModel just reading and returning the values from the static file to the tables?

2
  • What's the model type declared in the view? I think you should do JsonConvert.DeserializeObject<Mstypes>(json) Commented Sep 15, 2020 at 20:07
  • @ChetanRanpariya I updated the snip also, Mstypes Commented Sep 15, 2020 at 20:15

2 Answers 2

1

It is very clear from the error message, the view accept a Mstypes type model, while you pass a Mstype to it.

So, you can encapsulate mstypes convert it to Mstypes type in your controller.

public IActionResult Run()
{
    var json = "{ 'CIS':11,'PPMS':12,'RRMS':6,'SPMS':11,'not_sure':10}";
    var mstype1 = JsonConvert.DeserializeObject<Mstype>(json);
    Mstypes mstypes = new Mstypes
    {
        mstypes = new List<Mstype> { mstype1 }
    };

    return View(mstypes);
}

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Great, Works! Thank you!
1

What if you change:

var mstypes = JsonConvert.DeserializeObject<Mstype>(json);

to

var mstypes = JsonConvert.DeserializeObject<Mstypes>(json);

Your .json file should be something like:

{
  mstypes : [
    {"CIS":11,"PPMS":12,"RRMS":6,"SPMS":11,"not_sure":10},
    {"CIS":11,"PPMS":12,"RRMS":6,"SPMS":11,"not_sure":10},
    {"CIS":11,"PPMS":12,"RRMS":6,"SPMS":11,"not_sure":10},
    {"CIS":11,"PPMS":12,"RRMS":6,"SPMS":11,"not_sure":10},
    {"CIS":11,"PPMS":12,"RRMS":6,"SPMS":11,"not_sure":10},
  ]
}

4 Comments

Thank you! But it returned Null Value! <ArgumentNullException: Value cannot be null. (Parameter 'source') >
Your json file do not reflect the schema you want, and use in your View
what it should be?
To match the json above, the type should be Mstype[] for both the @Model line and the call to DeserializeObject

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.