如何在MVC3中呈现局部视图

Viv*_*tel 5 c# asp.net-mvc-3

我试图在我的应用程序中呈现局部视图,无法显示值.这是我的View的样子.

我的主要索引视图

 <div id="RPPricingNameModel">
     @Html.Partial("RPPricingPlanNames")
 </div>
<script type="text/javascript">
    $("#RPPricingNameModel").load("/Home/GetPlanNameModel");    
</script>
Run Code Online (Sandbox Code Playgroud)

局部视图

@model PlanNameModel     
<table style= "vertical-align:top; left:0px; top:0px; position:absolute; border-width:1px; border-style:solid; border-color:Green;  width:130px; text-align:left;">    
    <tr>
        <td style=" font-size:15px; font-weight:bold; color:Black;">            
            @Model.Header
           <div>            
                  @Html.LabelFor(x => x.Header)               
           </div>   
        </td>
    </tr>
<table>
Run Code Online (Sandbox Code Playgroud)

这是返回视图的控制器.

public ActionResult GetPlanNameModel()
{
   PlanNameModel planNameModel = new PlanNameModel();
   planNameModel.Header = "Plans";
   //return View(planNameModel);
   return PartialView(planNameModel);   
}
Run Code Online (Sandbox Code Playgroud)

这是Model的代码

public class RPPricingPlanNameModel
{
  public string Header { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在TD中显示该值时,它不会显示任何内容.你能帮我解决这个问题吗?

Bas*_*nni 9

如果你要做的就是使用partialViews而不是用jquery动态加载它们,请看看我对这个问题的回答,它应该可以解决你的问题:

MVC3部分视图

您所要做的就是为索引创建一个ViewModel,其中包含部分视图所需的所有对象

控制器ActionMethod:

public ActionResult Index()
{
  PlanNameModel planNameModel = new PlanNameModel();
  planNameModel.Header = "Plans";
  ViewData.Model = new IndexVm{ PlanNameModel = planNameModel };
}
Run Code Online (Sandbox Code Playgroud)

视图模型:

   public class IndexVm
   {
     public PlanNameModel PlanNameModel { get; set; }
   }
Run Code Online (Sandbox Code Playgroud)

索引视图

@model IndexVm

@*Whatever index code you have*@

@Html.Partial("PlanPartial", Model.PlanNameModel)
Run Code Online (Sandbox Code Playgroud)

局部视图

@model PlanNameModel

<div>@Model.Header</div>
Run Code Online (Sandbox Code Playgroud)