2011-05-06 101 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