0

我有以下代码,由于某种原因,它无法按照我的预期工作。基本上我在网上找到了这个示例,并希望根据我的需要对其进行调整,但问题是我似乎无法在页面上显示 ImageList。

代码如下:-

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

            var imageList = $('#imageList');

            //hide the imageList initially
            imageList.hide();

    });

    var Project = {
        save: function () {
            var projectForm = $("#projectForm");
            if (projectForm.valid()) {
                projectForm.submit();
            }
            else {
                Project.onInvalidForm();
            }
        },
        onInvalidForm: function () {
            //CODE FOR INVALID FORM
        },
        onSaveBegin: function () {
            //CODE FOR SAVE BEGIN
        },
        onSaveSuccess: function () {
            //CODE FOR SAVE SUCCESS
            $('#imageList').fadeIn('slow');
        },
        onSaveFailure: function () {
            //CODE FOR SAVE FAILURE
        },
        onSaveComplete: function () {
            //CODE FOR SAVE COMPLETE
            $('#imageList').fadeIn('slow');
        }
    };
</script>


@using (Ajax.BeginForm(
    "/Create",
    new { },
    new AjaxOptions
    {
            InsertionMode = InsertionMode.Replace,
            OnComplete = "Project.onSaveComplete",
            OnFailure = "Project.onSaveFailure",
            OnSuccess = "Project.onSaveSuccess",
            OnBegin = "Project.onSaveBegin"
    },
    new { id = "projectForm", name = "projectForm" }))
    {
    <div >

我似乎无法指出出了什么问题,并且无法显示 imageList。代码正在通过onSaveSuccess: function ()并且onSaveCompete: function()因为我alert('')之前在代码中做了一个验证它实际上是从那里传递的。

感谢您的帮助和时间

@if(Model != null)
{
    @Html.Hidden("hfProjectId", Model.Project.ProjectID)
}

<div class="editor-label">
        @Html.LabelFor(m => m.Project.ProjectTitle)
 </div>
 <div class="editor-field">
        @Html.TextBoxFor(m => m.Project.ProjectTitle)
        @Html.ValidationMessageFor(m => m.Project.ProjectTitle)
 </div>
 <div class="editor-label">
        @Html.LabelFor(m => m.Project.ProjectText)
 </div>
 <div class="editor-field">
        @Html.TextBoxFor(m => m.Project.ProjectText)
        @Html.ValidationMessageFor(m => m.Project.ProjectText)
 </div>
 <p>
        <input type="submit" value="Create" />
 </p>
</div>
}


<div id='imageList'>
@{
    if (Model != null)
    {
        if(Model.Project.ProjectID > 0)
        {
            Html.Partial("~/Views/Shared/ImageUpload.cshtml", new MvcImageUploaderPartial.ViewModels.ImageModel(Model.Project.ProjectID, 2, "Edit"));
        }
        else
        {
            Html.Partial("~/Views/Shared/ImageUpload.cshtml", new MvcImageUploaderPartial.ViewModels.ImageModel(0, 0, "Create"));
        }
    }
}
</div>

** * ** *更新* ** * ** * ** * ** * ** * ** * ** * 项目控制器:-

        [HttpPost]
    public ActionResult Create(Project project)
    {
        ProjectModel viewModel = new ProjectModel();

        if (ModelState.IsValid)
        {
            db.Projects.Add(project);
            db.SaveChanges();
            viewModel.Project = project;

            return View("Create", viewModel);
        }

        return View(project);
    }

图像控制器:-

        public ActionResult Index()
    {
        ImageModel model = new ImageModel();

        return RedirectToAction("Create", "Project", model);
    }

    public ActionResult Save()
    {
        IEnumerable<Image> ImageList;

        //if (!String.IsNullOrEmpty(TempData["ProjectId"].ToString()))
        //    iProjId = Convert.ToInt32(TempData["ProjectId"]);

        foreach (string name in Request.Files)
        {
            var file = Request.Files[name];

            string fileName = System.IO.Path.GetFileName(file.FileName);

            Image image = new Image(fileName, Request["description"]);
            ImageModel model = new ImageModel();

            ImageList = model.Populate();
            model.Add(ImageList, image, file);
        }

        return RedirectToAction("Index", "Image");
    }
4

1 回答 1

0

我看到AjaxOptions你没有通过UpdateTargetId。您应该如下更新代码,

@using (Ajax.BeginForm(
    "/Create",
    new { },
    new AjaxOptions
    {
            UpdateTargetId = "imageList",
            InsertionMode = InsertionMode.Replace,
            OnComplete = "Project.onSaveComplete",
            OnFailure = "Project.onSaveFailure",
            OnSuccess = "Project.onSaveSuccess",
            OnBegin = "Project.onSaveBegin"
    }
    //..

还有一件更重要的事情是您在 中指定的元素UpdateTargetId应该在主视图中,而不是在将由 ajax 操作返回的部分视图中。另外,请确保您参考了不显眼的 ajax 库。

于 2012-06-11T13:11:10.857 回答