简体   繁体   English

MVC局部视图未呈现

[英]MVC Partial View not rendering

Partial view is not rendering while passing ViewModel.. Its rendering without ViewModel. 通过ViewModel时不呈现局部视图。没有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. 我的意思是,如果我保留@ Html.Partial(“ PartialClientIndex”),则呈现它,并且当我传递ViewModel时,它将直接转到Dispose,而不呈现局部视图。 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>

It seems that the variable vmclient that you are passing as model into your partial view is of type VMClient . 看来,要作为模型传递到部分视图的变量vmclient的类型为VMClient Though your partial view is expecting the IEnumerable<VMClient> type. 尽管您的部分视图需要IEnumerable<VMClient>类型。

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 并调整分配y变量的方式

var y = Model.IEClients;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM