简体   繁体   English

MVC 4 / ajax /部分视图

[英]MVC 4/ajax/partial view

After looking up related StackOverflow topics I did not find any appropriate solution I finally come up to post... initially: ajax works, I do have a server response of the html filled up table partialview, however it is not rendered at the specified of which id is "QuerySection". 在查找了相关的StackOverflow主题之后,我没有找到任何合适的解决方案,我终于来发布了……最初:ajax的工作原理是,我确实有html填满的表格partialview的服务器响应,但是未在指定的位置呈现其ID为“ QuerySection”。 scripts bundled: "~/Scripts/jquery-1.9.1.js", "~/Scripts/MicrosoftAjax.js", "~/Scripts/MicrosoftMvcAjax.js" as well as jquery.unobtrusive-ajax.js 捆绑的脚本:“〜/ Scripts / jquery-1.9.1.js”,“〜/ Scripts / MicrosoftAjax.js”,“〜/ Scripts / MicrosoftMvcAjax.js”以及jquery.unobtrusive-ajax.js

what am I doing wrong? 我究竟做错了什么?

controller's action partialview added filename below 控制器的动作partialview在下面添加了文件名

   [HttpPost]
    public ActionResult LogsPartial(string[] choices)
    {
        var datebegin = choices[0].Length==0?new DateTime(2014,1,1): DateTime.Parse(choices[0]);
        var dateend = choices[1].Length == 0 ? new DateTime(2150, 1, 1) : DateTime.Parse(choices [1]); 
        var lstLogs = db.Logs.Include("Users").Where(x => x.DateOfAction >= datebegin &&.DateOfAction <= dateend).ToList();
        if (lstLogs.Any())
        {
            return PartialView("LogsPartial", lstLogs);
        }
        else
            return PartialView("LogsPartial",null);

    }

partialview >> LogsPartial.cshtml 局部视图>> LogsPartial.cshtml

@model IEnumerable<ContiRFQ.Models.Logs>

    @if (Model == null || Model.Count() == 0)
    {
        <p>No results found</p>
    }
    else
    {
        <h2>Logs</h2>

            <table>
                <tr>

                    <th>
                        Account
                    </th>
                    <th>
                       Action
                    </th>
                    <th>
                       Date of Action
                    </th>
                    <th></th>
                </tr>

                @foreach (var item in Model)
                {
                    <tr>

                        <td>
                        @Html.DisplayFor(modelItem => item.Users.Account)   
                        </td>
                        <td>
                             @Html.DisplayFor(modelItem => item.Action)
                        </td>
                        <td>
                          @item.DateOfAction.ToString("MMMM dd yyyy, HH:mm")  
                        </td>

                    </tr>
                }

    </table>

} }

and the index.cshtml 和index.cshtml

<script type="text/javascript">
$(document).ready(function () {
    $('#submit').click(function () {

        var dates = [$('#BeginDate').val(), $('#EndDate').val()];
        var json = JSON.stringify(dates);

        $.ajax({
            url: "@Url.Action("LogsPartial", "Logs")",

            type: 'POST',
            cache: false,
            contentType: 'application/json; charset=utf-8',
            data: json,
            dataType: 'json',
            success: function (data) 
            {
                $('#QuerySection').html(data);
            },
            error: function () {
                console.trace();
            }
        });
    });
});

 <div id="QuerySection"> </div>         

I would try changing the action method to a PartialViewResult (explicitly) instead of an ActionResult. 我会尝试将操作方法​​更改为PartialViewResult(显式),而不是ActionResult。 Then you just need to do 那你只需要做

return PartialView(lstLogs);

I am guessing that the OS handles it based on the return type specific in the method signature rather than what you actually return. 我猜想OS是根据方法签名中特定的返回类型而不是您实际返回的内容来处理它的。

Thank you all guys, after some hours struggling /trying it turned out that an evident parameter of .ajax function was wrong. 谢谢大家,经过几个小时的努力/尝试,事实证明.ajax函数的明显参数是错误的。 datatype:json, though I was expecting html ...... 数据类型:json,尽管我期待html ......

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

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