Jen143
Jen143

Reputation: 835

Insert Partial View to a another Partial View

This is the main view Dept_Manager_Approval.cshtml where I have put a modal to show data.

<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>

In this partial view which just a modal, ViewAccessStatus.cshtml , I have inserted in here another partial view.

<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>

COntroller:

       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>

When I run this, I've got this error

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]'.

Any ideas how will I insert another partial view?

Upvotes: 0

Views: 1293

Answers (1)

user3559349
user3559349

Reputation:

@Html.Partial() renders a partial view. It does not call an action method that in turn renders the partial. In your case

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

is rendering a partial view named Batch_Requestor1.cshtml and passing it a model defined by new {id= Model.carf_id } (and anonymous object) but that view expects a model which is IEnumerable<PETC_CARF.Models.Batch_CARF>.

Instead, you need to use

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

which calls the method public ActionResult Batch_Requestor1(int id = 0) and passes it the value of Model.carf_id, which will in turn render the partial view.

Upvotes: 3

Related Questions