Ajax.Beginform刷新整个页面

时间:2015-07-16 21:40:14

标签: ajax partial-views

它重定向到整个新页面,而不仅仅是更新当前的页面,我有不引人注意的脚本和web.config的东西。为什么这不起作用?

<div id="preview-image-placeholder"></div>

@foreach (var item in Model)
{
    using (Ajax.BeginForm("PreviewImage/" + @item.Id.ToString(), "Music", null, new AjaxOptions { HttpMethod = "Post", InsertionMode = InsertionMode.Replace, UpdateTargetId = "preview-image-placeholder" },
                                                      new { @enctype = "multipart/form-data", @item.Id }))
    {
        @Html.AntiForgeryToken()

        <input type="file" class="upload-image" id="upload-image-file_@item.Id" name="file" accept="image/*">
        <button class="btn btn-default" id="btn-preview_@item.Id" style="display: none" type="submit"></button>
    }
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

控制器:

     [ValidateAntiForgeryToken]
     [HttpPost]
        public PartialViewResult PreviewImage(HttpPostedFileBase file, int id)
        {
            //Do other code stuff
                return PartialView("_DisplayImage", song);
        }

_DisplayImage局部视图:

@model IEnumerable<MusicSite.Models.UploadedSong>
@using MusicSite.CustomHtmlHelpers;

@foreach(var item in Model)
{
    if (@item.CoverImageBytes != null)
    {
        @Html.ImageFor(x => item.CoverImageBytes, @item.SongName + " Image", new { @id = "preview-image" })
    }
}

1 个答案:

答案 0 :(得分:0)

除了不显眼的脚本,您还必须添加Microsoft Ajax库(MicrosoftAjax.js和MicrosoftMvcAjax.js),以便Ajax.BeginForm正常工作。

此致