2016-05-17 153 views
1

我在另一個局部視圖內部有一個局部視圖,當第一次運行應用程序按預期方式加載時,但當您單擊以重新加載視圖以將模型推入模型時,它會呈現爲其自身完全獨立的看法,如果它不是一個部分。局部視圖不渲染內部視圖

我打電話是Ajax表格內,像這樣(在動作鏈接點擊後,_GetSearchModal法):

<div id="modelSearch"> 
    <div class="panel panel-default"> 
     <div class="panel-heading"> 
      <h3 class="panel-title"> 
       <i class="fa fa-search"></i> Search by Model/Manufacturer 
      </h3> 
     </div> 
     <div class="panel-body"> 
      @using (Ajax.BeginForm("_GetSearch", "Home", new AjaxOptions() {UpdateTargetId = "modelSearch"})) 
      { 
       @Html.AntiForgeryToken() 
       <div class="input-group"> 
        @Html.TextBox("search", null, new {id = "name", @class = "form-control", placeholder = "Please enter a manufacturer or model"}) 
        <span class="input-group-btn"> 
         <button id="search" class="btn btn-default" type="submit"><i class="fa fa-search"></i></button> 
        </span> 
       </div> 
       if (Model != null) 
       { 
        <div id="searchResults" class="fade-in two"> 
         @foreach (var s in Model) 
         { 
          <div class="result"> 
           @switch (s.ResultType) 
           { 
            case "Man": 
             <a href="#">@s.Manufacturer</a> 
             break; 
            case "Mod": 
             @Html.ActionLink(s.Manufacturer + Html.Raw(s.Model), "_GetSearchModal", "Home", new {id = s.MachineId}, new {toggle = "modal", data_target = "#MachineModal"}) 
             <img src="~/Images/General/Tier/@(s.TierId).png" alt="Tier @s.TierId"/> 
             break; 
           } 

          </div> 

         } 
        </div> 
       } 
      } 
     </div> 
    </div> 
</div> 

<!-- Product Modal --> 
<div class="modal fade" id="MachineModal" tabindex="-1" role="dialog" aria-labelledby="MachineModalLabel"> 
    @Html.Partial("_SearchModal", new MachineModal()) 
</div> 

和視圖本身應該加載不同的視圖模型(MachineModal):

@model SpecCheck.Portals.Web.UI.ViewModels.MachineModal 

@if (Model != null) 
{ 

     <div class="modal-dialog" role="document"> 
      <div class="modal-content"> 
       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
        <h4 class="modal-title" id="MachineModalLabel">@Model.Manufacturer @Model.Model</h4> 
       </div> 
       <div class="modal-body"> 
        <div class="row"> 
         <div class="col-md-6"> 
          <img src="~/Images/@Model.Manufacturer/logo.png" alt="@Model.Manufacturer" /><br /> 
          <a href="#" type="button" class="btn button">Wiki</a> 
         </div> 
         <div class="col-md-6"> 
          @Model.Catagory1 | @Model.Category2<br /><br /> 
          <span class="modal-em">Region: </span> @Model.Region<br /> 
          <span class="modal-em">Status: </span>@Model.Status<br /> 
          <span class="modal-em">Spec Date: </span>@Model.SpecDate 
         </div> 
        </div> 
       </div> 
       <div class="modal-footer"> 
        <a href="#" type="button" class="btn button">View</a> 
        <a href="#" type="button" class="btn button">Quick Compare</a> 
        <a href="#" type="button" class="btn button">Compare</a> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       </div> 
      </div> 
     </div> 
} 

而在 「家庭控制器」 做這個動作是:

public ActionResult _GetSearchModal(string machineId) 
{ 
    using (var db = new SpecCheckDbContext()) 
    { 
     MachineModal machine = new MachineModal(); 
     var searchHelper = new SearchHelper(db); 

     //Get Machine Details 
     var dbResults = searchHelper.SearchModal(Convert.ToInt32(machineId)); 

     machine.Model = dbResults.Model; 
     machine.Catagory1 = dbResults.Catagory1; 
     machine.Category2 = dbResults.Category2; 
     machine.Manufacturer = dbResults.Manufacturer; 
     machine.Region = dbResults.Region; 
     machine.SpecDate = dbResults.SpecDate; 
     machine.Status = dbResults.Status; 
     machine.MachineId = dbResults.MachineId; 
     machine.ManufacturerId = dbResults.ManufacturerId; 

     var model = machine; 

     return PartialView("_SearchModal", model); 
    } 
} 

第一我檢查過的東西是腳本,當佈局頁面加載時它們都處於適當的位置,所以它不是腳本問題。不知道該怎麼改變,甚至嘗試在這一點上,所以任何建議歡迎。

+1

如果它在新視圖中呈現你的部分,這意味着你沒有包含正確的腳本('jquery-unobtrusive-ajax.js'),而你正在進行提交,而不是ajax提交。但它是'UpdateTargetId =「modelSearch」'或者它應該是'UpdateTargetId =「MachineModal」'? –

回答

3

在AJAX形式:

_GetSearch => _GetSearchModal(name of the action)

嘗試返回machine的局部視圖?也許看到在視圖層次結構中,是否有第二個_SearchModal局部視圖,返回?