尝试在ASP MVC 3中呈现部分视图时出现问题

时间:2011-05-06 10:39:10

标签: c# .net asp.net-mvc-3

现在我在使用ASP MVC 3时遇到一个问题,试图在AJAX请求完成后重新发送存储在PartialView中的内容。这是我的两个观点的代码:

@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 个答案:

答案 0 :(得分:1)

部分视图未显示,因为您的目标div未呈现。不要在主视图中使用@ Html.Partial(),而是插入以下内容:

<div id="results" />

这将创建目标,您的部分将被插入。