繁体   English   中英

尝试在ASP MVC 3中呈现PartialView时出现问题

[英]Problem when trying to render a PartialView in ASP MVC 3

我现在遇到ASP MVC 3的问题,当尝试在AJAX请求完成后尝试撕裂存储在PartialView中的a时。 这是我的两个视图的代码:

@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()的自定义属性),并且正在返回数据,那么为什么不呈现视图?

由于未渲染目标div,因此未显示局部视图。 而不是在主视图中使用@ Html.Partial(),请插入以下内容:

<div id="results" />

这将创建目标,将您的局部插入其中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM