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