传递模型的问题

时间:2015-05-26 14:39:55

标签: c# asp.net asp.net-mvc razor

更新 我指向控制器中的错误视图。

不确定我错过了什么,但我传递的项目是视图所需的模型。单击“删除”以删除条目时。

The model item passed into the dictionary is of type PAL.Intranet.Models.FeeSchedule_CCGNInsured, but this dictionary requires a model item of type System.Collections.Geeric.IEnumerable1[PAL.Intranet.Models.FeeSchedule_CCGNInsured]

受保人

@model IEnumerable<PAL.Intranet.Models.FeeSchedule_CCGNInsured>

@{
    ViewBag.Title = "CCGN Insured Fee Schedule";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<link href="~/Content/CSS/MetroButtonWidth.css" rel="stylesheet" />

<h2>CCGN Insured Fee Schedule</h2>
<hr class="bg-dark" />

@if (User.IsInRole("Intranet Admins"))
{
    <p>
        <button class="button small-button info buttonWidth100" onclick="location.href='@Url.Action("CCGNInsuredCreate", "FeeSchedule")'">Create New</button>
        <br />  
    </p>
}

<table class="table hovered bordered striped border">
    <tr>
        <th class="fg-white bg-dark">
            CPT
        </th>
        <th class="fg-white bg-dark">
            Description
        </th>
        <th class="fg-white bg-dark">
            Insured Office Fee
        </th>
        <th class="fg-white bg-dark">
            10% Coin
        </th>
        <th class="fg-white bg-dark">
            20% Coin
        </th>
        <th class="fg-white bg-dark">
            30% Coin
        </th>
        <th class="fg-white bg-dark">
            Insured Non Office Fee
        </th>
        <th class="fg-white bg-dark">
            10% Coin
        </th>
        <th class="fg-white bg-dark">
            20% Coin
        </th>
        <th class="fg-white bg-dark">
            30% Coin
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td class="text-small">
            @Html.DisplayFor(modelItem => item.CPT)
        </td>
        <td class="text-small">
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td class="text-small">
            @Html.DisplayFor(modelItem => item.InsuredOfficeFee)
        </td>
        <td class="text-small">
            @Html.DisplayFor(modelItem => item.IOF10)
        </td>
        <td class="text-small">
            @Html.DisplayFor(modelItem => item.IOF20)
        </td>
        <td class="text-small">
            @Html.DisplayFor(modelItem => item.IOF30)
        </td>
        <td class="text-small">
            @Html.DisplayFor(modelItem => item.InsuredNonOfficeFee)
        </td>
        <td class="text-small">
            @Html.DisplayFor(modelItem => item.INOF10)
        </td>
        <td class="text-small">
            @Html.DisplayFor(modelItem => item.INOF20)
        </td>
        <td class="text-small">
            @Html.DisplayFor(modelItem => item.INOF30)
        </td>

        @if (User.IsInRole("Intranet Admins"))
        {
            <td>
                <button class="button small-button info buttonWidth75" onclick="location.href='@Url.Action("CCGNInsuredEdit", "FeeSchedule", new { id = item.ID })'">Edit</button>
                <button class="button small-button danger buttonWidth75" onclick="location.href='@Url.Action("CCGNInsuredDelete", "FeeSchedule", new { id = item.ID })'">Delete</button>
            </td>
        }
    </tr>
}

</table>


<button class="button small-button danger buttonWidth75" onclick="location.href='@Url.Action("CCGNInsuredDelete", "FeeSchedule", new { id = item.ID })'">Delete</button>

控制器

public ActionResult CCGNInsuredDelete(Guid? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        FeeSchedule_CCGNInsured FeeSchedule_CCGNInsured = db.FeeSchedule_CCGNInsured.Find(id);
        if (FeeSchedule_CCGNInsured == null)
        {
            return HttpNotFound();
        }
        return View("/Views/FeeSchedule/CCGN/InsuredIndex.cshtml", FeeSchedule_CCGNInsured);
    }

删除视图

@model PAL.Intranet.Models.FeeSchedule_CCGNInsured

1 个答案:

答案 0 :(得分:1)

您正在将FeeSchedule_CCGNInsured的单个对象传递到您的视图InsuredIndex.cshtml,该对象必须包含以下定义:

@model IEnumerable<PAL.Intranet.Models.FeeSchedule_CCGNInsured>

(或从IEnumerable派生的集合)

更改FeeSchedule_CCGNInsured视图以使用FeeSchedule_CCGNInsured的单个实例,或更新您的回复以将其转发到您提及的删除视图:

return View("/Views/FeeSchedule/CCGN/delete.cshtml", FeeSchedule_CCGNInsured);