【问题标题】:Problem when trying to render a PartialView in ASP MVC 3尝试在 ASP MVC 3 中呈现局部视图时出现问题
【发布时间】:2011-08-20 02:11:00
【问题描述】:

在 AJAX 请求完成后尝试渲染存储在 PartialView 中的 ASP MVC 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() 的自定义属性)并且它正在返回数据,那么为什么视图没有呈现?

【问题讨论】:

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


    【解决方案1】:

    部分视图没有出现,因为你的目标 div 没有被渲染。不要在主视图中使用@Html.Partial(),而是插入以下内容:

    <div id="results" />
    

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

    【讨论】:

    • 谢谢,成功了。但是,现在我有以下疑问,ASP MVC 是如何知道它必须在该 div 中呈现部分视图 SearchResults 的?
    • @groovejet 因为您将 UpdateTargetId 设置为该 div 的 id。
    猜你喜欢
    • 2011-11-17
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2011-07-23
    • 2012-09-04
    • 1970-01-01
    相关资源
    最近更新 更多