将局部视图插入另一个局部视图

Insert Partial View to a another Partial View

这是主视图Dept_Manager_Approval.cshtml,我在其中放置了一个模式来显示数据。

<td>
    <i title="View Details">
    @Ajax.ActionLink(" ", "ViewAccessStatus", new { id = item.request_access_id },
    new AjaxOptions
    {
        HttpMethod = "Get",
        InsertionMode = InsertionMode.Replace,
        UpdateTargetId = "edit-div",
    }, new { @class = "fa fa-eye btn btn-success approveModal sample" })</i> 
</td>

在这个只是模态的局部视图中,ViewAccessStatus.cshtml,我在这里插入了另一个局部视图。

<div>
    <h2><span class ="label label-success">Request Creator</span> &nbsp; </h2>
    @if (Model.carf_type == "BATCH CARF") 
    { 
        @Html.Partial("Batch_Requestor1", new {id= Model.carf_id })

    }else{
       <h4><span class ="label label-success">@Html.DisplayFor(model=>model.created_by)</span></h4>
    }
</div>

控制器:

       public ActionResult Batch_Requestor1(int id = 0)
        {
            var data = db.Batch_CARF.Where(x => x.carf_id == id && x.active_flag == true).ToList();

            return PartialView(data);
        }

Batch_Requestor1.cshtml

@model IEnumerable<PETC_CARF.Models.Batch_CARF>

@{
    ViewBag.Title = "All Requestors";
}

<br/><br/>
<table class="table table-hover">
    <tr class="success">
        <th>
            @Html.DisplayName("Full Name")
        </th>
        <th>
            @Html.DisplayName("Email Add")
        </th>
        <th>
            @Html.DisplayName("User ID")
        </th>             
    </tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.fname) - @Html.DisplayFor(modelItem => item.lname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.email_add)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.user_id)
        </td>
    </tr>
}
</table>

当我运行这个的时候,我遇到了这个错误

The model item passed into the dictionary is of type '<>f__AnonymousType01[System.Int32]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[PETC_CARF.Models.Batch_CARF]'.

知道如何插入另一个局部视图吗?

@Html.Partial() 呈现局部视图。它不会调用反过来呈现部分的操作方法。你的情况

@Html.Partial("Batch_Requestor1", new {id= Model.carf_id })

正在呈现一个名为 Batch_Requestor1.cshtml 的局部视图,并向其传递一个由 new {id= Model.carf_id }(和匿名对象)定义的模型,但该视图需要一个模型 IEnumerable<PETC_CARF.Models.Batch_CARF>.

相反,您需要使用

@Html.Action("Batch_Requestor1", new {id= Model.carf_id })

调用方法 public ActionResult Batch_Requestor1(int id = 0) 并将 Model.carf_id 的值传递给它,这将依次呈现局部视图。