C# Web API JSON - 详细信息视图问题

时间:2021-07-24 22:28:33

标签: c# asp.net-mvc asp.net-web-api

我正在尝试显示我的详细信息页面,该页面具有正确的 URL 和 ID:https://localhost:44319/Citation/Details/00007082303,我收到以下错误。这适用于 Postman,所以我知道 URL 正在返回数据。

Server Error in '/' Application.
The model item passed into the dictionary is of type 'CPA.Models.Root', but this dictionary requires a model item of type 'CPA.Models.CitationDetail'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'CPA.Models.Root', but this dictionary requires a model item of type 'CPA.Models.CitationDetail'.

这是我的 JSON:

public class AlertMessage
{
    public string Title { get; set; }
    public string Message { get; set; }
    public string Severity { get; set; }
}

public class ItemLine
{
    public string Description { get; set; }
    public int BilledAmount { get; set; }
    public int PaidAmount { get; set; }
}

public class CitationDetail
{
    public string CitationNumber { get; set; }
    public string DefendantName { get; set; }
    public string DateofBirth { get; set; }
    public string VehicleTagNumber { get; set; }
    public string CaseType { get; set; }
    public string CaseStatus { get; set; }
    public string AppearanceDate { get; set; }
    public bool IsPayable { get; set; }
    public int FineSuspended { get; set; }
    public int FineServed { get; set; }
    public List<AlertMessage> AlertMessages { get; set; }
    public List<ItemLine> ItemLines { get; set; }
}

public class Root
{
    public List<CitationDetail> CitationDetails { get; set; }
    public int CitationCount { get; set; }
    public bool SuccessfulSearch { get; set; }
    public string ErrorMessage { get; set; }
}

这是我这次调用的控制器:

public async Task<ActionResult> Details(int id)
{                          
    //var CitInfo="";
    using (var client = new HttpClient())
    {
        //Passing service base url
        client.BaseAddress = new Uri(Baseurl);
        client.DefaultRequestHeaders.Clear();
        //Define request data format
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage Res = await client.GetAsync("api/Juris/Citation/" + id);
        //Checking the response is successful or not which is sent using HttpClient

        //Storing the response details recieved from web api
        var EmpResponse = Res.Content.ReadAsStringAsync().Result;

        //Deserializing the response recieved from web api and storing into the citation list
        //var empResponseObj = JsonConvert.DeserializeObject<CitationDetail>(EmpResponse);
        //var url = Baseurl + "api/Juris/Citation/" + id;
        //var store = JsonConvert.DeserializeObject<CitationDetail>(await client.GetStringAsync(url));
        //return View(store);
        var empResponseObj = JsonConvert.DeserializeObject<Root>(EmpResponse);
        return View(empResponseObj);
        //returning the citation list to view
    }
}

这是详细信息 html 页面:

 @model IEnumerable<CPA.Models.CitationDetail>

 @{
  ViewBag.Title = "Index";
}

    <table class="table">
<tr bgcolor="#e0e0e0">
    <th>
        Citation#
    </th>
    <th>
        Defendant
    </th>
    <th>
        DOB
    </th>
    <th>
        Vehicle&nbsp;Tag
    </th>
    <th>
        Case&nbsp;Type
    </th>
    <th>
        Case&nbsp;Status
    </th>
    <th>
        Court&nbsp;Date
    </th>
    <th>
        @Html.DisplayNameFor(model => model.IsPayable)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.FineSuspended)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.FineServed)
    </th>
    <th></th>
</tr>

@foreach (var item in Model)
{
   <tr onmouseover="ChangeBackgroundColor(this)" onmouseout="RestoreBackgroundColor(this)">


       <td>
           @Html.ActionLink(item.CitationNumber, "Details", new { id = item.CitationNumber })  
       </td>
        <td>
            @Html.DisplayFor(modelItem => item.DefendantName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DateofBirth)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.VehicleTagNumber)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CaseType)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CaseStatus)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.AppearanceDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IsPayable)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FineSuspended)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FineServed)
        </td>
        <td>
            @*@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })*@

        </td>
    </tr>
}

0 个答案:

没有答案