簡體   English   中英

如何在MVC3中呈現局部視圖

[英]How to render Partial View in MVC3

我試圖在我的應用程序中呈現部分視圖,但無法顯示值。 這是我的View的樣子。

我的主要索引視圖

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

局部視圖

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

這是返回視圖的控制器。

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

這是Model的代碼

public class RPPricingPlanNameModel
{
  public string Header { get; set; }
}

當我嘗試在TD中顯示該值時,它不會顯示任何內容。 你能幫我解決這個問題嗎?

如果你要做的就是使用partialViews而不是用jquery動態加載它們,請看看我對這個問題的回答,它應該可以解決你的問題:

MVC3部分視圖

您所要做的就是為索引創建一個ViewModel,其中包含部分視圖所需的所有對象

控制器ActionMethod:

public ActionResult Index()
{
  PlanNameModel planNameModel = new PlanNameModel();
  planNameModel.Header = "Plans";
  ViewData.Model = new IndexVm{ PlanNameModel = planNameModel };
}

視圖模型:

   public class IndexVm
   {
     public PlanNameModel PlanNameModel { get; set; }
   }

索引視圖

@model IndexVm

@*Whatever index code you have*@

@Html.Partial("PlanPartial", Model.PlanNameModel)

局部視圖

@model PlanNameModel

<div>@Model.Header</div>

看起來你缺少你在jquery選擇器中尋找的選擇器

你需要添加:

<div id="RPPricingNameModel"></div>

到您的DOM

你的@m.Header也應該是@ Model.Header

最后你的javascript加載部分視圖不應該你的局部視圖中,它應該在主視圖上,你想加載部分視圖到

用以下內容替換索引:

<div id="RPPricingNameModel"></div>

<script type="text/javascript">
  $(function(){$("#RPPricingNameModel").load("/Home/GetPlanNameModel");});
</script>

空引用是由對partial by的初始調用引起的

@Html.Partial("RPPricingPlanNames")

沒有所需的型號

換行$("#RPPricingNameModel").load("/Home/GetPlanNameModel"); 准備好文檔內部,以便只在加載完整的dom后執行

$(function(){

 $("#RPPricingNameModel").load("/Home/GetPlanNameModel");

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM