试图传递模型进行查看但出现异常

时间:2018-10-26 08:16:59

标签: c# asp.net-mvc view

我的问题

我是新来的,除了以下例外情况,我需要一些帮助。我反序列化了一个JSON数组,并根据我所做的所有阅读,我相信我会将模型传递给视图。

读取异常可以推断出模型,根对象和模型。调查之间存在某种不匹配。

我试图将Rootboject转换为List,但是会生成另一个异常,我认为这可以解决IEnumerable问题。

所以我认为我有两个问题,我认为我的模型由于某种原因不匹配,但是视图期望一个列表,但我没有提供列表。值得注意的是,我已经从模型中自动生成了视图。

An unhandled exception occurred while processing the request.
InvalidOperationException: The model item passed into the ViewDataDictionary 
is of type 'AgeCareTech.Models.Rootobject', but this ViewDataDictionary 
instance requires a model item of type 
'System.Collections.Generic.IEnumerable`1[AgeCareTech.Models.Survey]'.

模型

namespace AgeCareTech.Models
{
public class Rootobject
{
    [JsonProperty("surveys")]
    public Survey[] surveys { get; set; }
}

public class Survey
{
    [JsonProperty("survey_id")]
    public int survey_id { get; set; }

    [JsonProperty("title")]
    public string title { get; set; }

    [JsonProperty("created_on")]
    public DateTime created_on { get; set; }

    [JsonProperty("number_of_questions")]
    public int number_of_questions { get; set; }

    [JsonProperty("number_of_responses")]
    public int number_of_responses { get; set; }
}

}

查看

@model IEnumerable<AgeCareTech.Models.Survey>

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

<h2>Index</h2>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.title)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.created_on)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.number_of_questions)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.number_of_responses)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.created_on)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.number_of_questions)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.number_of_responses)
            </td>
            <td>
                <a asp-action="Edit" asp-route-id="@item.survey_id">Edit</a> |
                <a asp-action="Details" asp-route-id="@item.survey_id">Details</a> |
                <a asp-action="Delete" asp-route-id="@item.survey_id">Delete</a>
            </td>
        </tr>
}
    </tbody>
</table>

控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using AgeCareTech.Models;

namespace AgeCareTech.Controllers
{
    public class SurveyHeroSurveyController : Controller
    {
        public async Task<IActionResult> Index()
        {
            string uri = "http://www.mocky.io/v2/5bcb430c2f0000730075beb1";

            Rootobject SurveyHeroInfo = new Rootobject();

            using (HttpClient SurveyHeroAPI = new HttpClient())
            {
                SurveyHeroAPI.BaseAddress = new Uri(uri);
                SurveyHeroAPI.DefaultRequestHeaders.Clear();
                SurveyHeroAPI.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage Response = await SurveyHeroAPI.GetAsync(uri, HttpCompletionOption.ResponseContentRead);

                if (Response.IsSuccessStatusCode)
                {
                    var SurveyHeroResponse = Response.Content.ReadAsStringAsync().Result;

                    SurveyHeroInfo = JsonConvert.DeserializeObject<Rootobject>(SurveyHeroResponse);
                }
            }

            return View(SurveyHeroInfo);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您告诉您的看法是期望的:

@model IEnumerable<AgeCareTech.Models.Survey>

...但是您给它一个Rootobject对象。 Rootobject不是IEnumerable<Survey>。该错误信息是明确的并且非常正确。尝试告诉您的观点期望使用Rootobject

@model Rootobject

答案 1 :(得分:1)

该消息很清楚-视图需要一个IEnumerable<AgeCareTech.Models.Survey>类型的模型,但是您正在发送AgeCareTech.Models.Rootobject。因此,代替

return View(SurveyHeroInfo);

尝试

return View(SurveyHeroInfo.surveys);    

或者,按照@kaffekopp的建议,更改视图的模型。