2011-05-06 132 views
1

當嘗試在AJAX請求完成後存儲在PartialView中時,ASP MVC 3現在有問題。這裏是我的兩個視圖代碼:嘗試在ASP MVC中呈現PartialView時出現問題3

@model ICollection<Foo.ViewModels.OrderSearchViewModel> 
@using Foo.Helpers 

@{ 
    ViewBag.Title = "Sales Order Lookup"; 
} 

@using (Ajax.BeginForm(new AjaxOptions() 
{ 
    HttpMethod = "GET", 
    UpdateTargetId = "results", 
    InsertionMode = InsertionMode.Replace 
})) 
{ 
    @*"Search","OrderSearch",FormMethod.Get*@ 
    <div id="Criteria" style="width: 800px;margin-left:10%"> 
     <table id="tblSearchCriteria" class="FunnyRedLine" width="100%"> 

       <!-- Many boring search fields here --> 

       <td style="width: 40%"> 
        <input type="submit" value="Search" class="Text" /> 
       </td> 
      </tr> 

     </table> 
    </div> 

} 

@Html.Partial("SearchResults",Model) 

這是局部視圖

@model ICollection<Foo.ViewModels.OrderSearchViewModel> 
@using Foo.Helpers 


@if (Model.Count > 0) 
{ 
    <div id="results" style="width: 800px; margin-left:10%"> 
     <table id="tbl"> 
      <tr> 
       <th>Sod Id</th> 
       <th>Sales Ref</th> 
       <th>SOP Ref</th> 
       <th>Order Date</th> 
       <th>Value</th> 
       <th>Status</th> 
       <th>Del Date</th> 
      </tr> 
     @foreach (var item in Model) 
     { 
      <tr> 
       <td width="30" align="center">@Html.DisplayFor(modelItem => item.Id)</td> 
       <td width="30" align="center">@Html.DisplayFor(modelItem => item.SalesPersonRef)</td> 
       <td width="200" align="center">@Html.DisplayFor(modelItem => item.SOPRef)</td> 
       <td width="100" align="left">@Html.FormatDate(item.OrderDate)</td> 
       <td width="100" align="right">@Html.FormatNumber(item.Value,"C2")</td> 
       <td width="300" align="left">@Html.DisplayFor(modelItem => item.Status)</td> 
       <td width="100" align="left">@Html.FormatDate(item.DelDate)</td> 
      </tr> 
     } 
     </table> 
    </div> 
} 

這是我的控制器

public class OrderSearchController : Controller 
    { 
     // 
     // GET: /OrderSearch/ 

    [Ajax(false)] 
    public ActionResult Index() 
    { 
     var viewModel = new List<OrderSearchViewModel>(); 

     ViewBag.SalesPeople = GetAllSalesPeople() 
     ViewBag.OrderTypes = GetAllOrderTypes() 

     return View(viewModel); 
    } 

    [Ajax(true)] 
    public ActionResult Index(string id, string startDate, string endDate, string postCode, string salesPersonId, string salesPersonRef, 
     string orderTypeId, string serialNo, string customerPO) 
    { 
      var service = new eSodSrv(); 
      var viewModel = service.GetHeaders(id.ToInt32(), salesPersonId.ToInt32(), orderTypeId.ToInt32(), startDate.ToDate(), endDate.ToDate(), postCode, customerPO, serialNo, salesPersonRef); 
      return PartialView("SearchResults",viewModel); 
    } 
} 

Ajax請求的工作,它是進入正確的方法(Ajax是檢查Request.IsAjaxRequest())的自定義屬性,並且它正在返回數據,那麼爲什麼視圖不會呈現?

回答

1

局部視圖沒有出現,因爲你的目標div沒有被渲染。除了使用@ Html.Partial()在主視圖,插入以下內容:

<div id="results" /> 

這將創建目標,你的部分將被插入。

+0

謝謝,這工作。然而,現在我有他下面的疑問,ASP MVC如何知道它必須在該div中呈現局部視圖SearchResults? – groovejet 2011-05-06 11:20:19

+0

@groovejet因爲您將UpdateTargetId設置爲該div的id。 – frennky 2011-05-06 11:24:45

相關問題