1

Partial view is not rendering while passing ViewModel.. Its rendering without ViewModel. I mean if I keep @Html.Partial("PartialClientIndex") then its rendering and when I pass ViewModel it directly going to Dispose without rendering partial view. I'm missing something here.. could you please help in this.

Main View:

    <div id="PartialClient">    
        @Html.Partial("PartialClientIndex", viewModelList)                     
   </div>

Action:

    [HttpPost]
    public ActionResult PartialClientIndex(int? SelectedGroupId)
    {
        int SkipRec = 0;
        int NextRec = 25;            
        VMClient vmclient = new VMClient();
        vmclient.IEClients = db.client.Where(cl => cl.Groups.id == SelectedGroupId).OrderBy(c => c.id).Skip(SkipRec).Take(NextRec).ToList();
        return PartialView("PartialClientIndex", vmclient);
    }

Partial View:

    @model IEnumerable<HostingManager.Models.VMClient>
   <table>
   <thead>
    <tr>
    <th style="width:25px; padding:10px;">
      Group
    </th>
    <th class="tbContent-Name">
        Client Name
    </th>
    <th class="tbContent-Name">
        Contact Person
    </th>
    <th class="tbContent-Name">
        Contact Email
    </th >        
    <th></th>
</tr>
</thead>
<tbody>
   @if(Model != null)        
   {
       var x = Model.Select(c=>c.IEClients).ToList();
       var y = x[0].ToList();

     //  var y = x[0]["item1"];
         foreach (var item in y) {
            <tr> 
                <td class="tbContent-Name">
                   @Html.DisplayFor(modelItem => item.Groups.name)
                </td>      
                <td class="tbContent-Name">
                    @Html.DisplayFor(modelItem => item.contactPerson)
                </td>
                <td class="tbContent-Name">
                    @Html.DisplayFor(modelItem => item.contactPerson)
                </td>
                <td class="tbContent-Name">
                    @Html.DisplayFor(modelItem => item.contactEmail)
                </td>        
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id=item.id }) |          
                    @Html.ActionLink("Delete", "Delete", new { id = item.id }, new { onclick = "return confirm('Are you sure you wish to delete this ?');" })
                </td>
            </tr>
            }
            }
    </tbody>

7
  • show the partial view code Commented Oct 11, 2014 at 15:58
  • I have added Partial view code @Ehsan Sajjad.. could you please advice Commented Oct 11, 2014 at 16:10
  • at first glance, you are attempting to pass a model to an ActionResult that is asking for an int? Commented Oct 11, 2014 at 16:47
  • @ethorn10 @Html.Partial won't call the action method, it just runs the cshtml file with that name Commented Oct 15, 2014 at 8:12
  • 1
    @user2254395 if it is going straight to dispose, I suspect there is an error occurring in the partial view - is viewModelList definitely an IEnumerable<VMClient>? how do you create this variable? Commented Oct 15, 2014 at 8:12

1 Answer 1

1

It seems that the variable vmclient that you are passing as model into your partial view is of type VMClient. Though your partial view is expecting the IEnumerable<VMClient> type.

You basically have to change the model type in your partial view to the following

@model HostingManager.Models.VMClient

and adjust the way you are assigning the y variable

var y = Model.IEClients;
Sign up to request clarification or add additional context in comments.

1 Comment

I've updated the model type definition line - seems that I accidentally wrote IEnumerable<VMClient> from the first try )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.