【发布时间】:2013-06-12 08:37:31
【问题描述】:
我有一个表单和提交按钮。通常,当执行提交操作时,它会返回到新页面并且不会更新部分视图。我在 jquery live 函数中使用 ajax 调用,但它仍然替换整个内容,而不是使用提供的模型返回部分视图。以下是我在_detailfile.cshtml 中的代码。应用程序上传文件但替换整个内容。
<div id="trainingFileDiv" style="overflow: auto; height: 470px;">
<table class="fileTable">
@foreach (var training in Model.TrainingList)
{
using (Html.BeginForm("fileForm", "Home/TempUpload", new { employeeId = Model.Id, trainId= @training.Id }, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
if (training.TrainingFile != null)
{
<tr>
<td>
</td>
</tr>
}
else
{
<tr>
<td class="view_detail_label" style="width: 250px;">
@training.Name.Name
</td>
<td>
<input type="file" name="@training.Id"/>
</td>
</tr>
<tr><td><input type="submit" name="Submit" id="Submit" value="Yükle" /></td>
</tr>
}
}
}
</table>
</div>
以下是我的脚本,仅更新 div 文件,但它不起作用。
$("fileForm").live("submit", function (event) {
if (form.valid()) {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
$('#trainingFileDiv').html(result);
}
});
}
event.preventDefault();
});
这是我在网上找到的另一个代码,它不会更新 div 而是更新所有内容。
$(function () {
$('fileForm').submit(function () {
if ($(this).valid()) {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
$('#trainingFileDiv').html(result);
}
});
}
return false;
});
});
我在这里缺少什么? 谢谢你的帮助
EDIT1
当部分视图从控制器返回时,我得到Uncaught ReferenceError: $ is not defined fileForm:7
EDIT2 函数对所有表单进行 ajaxify。我在edit1中得到错误
$(function () {
$('form').submit(function () {
if ($(this).valid()) {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
$('#trainingFileDiv').append(result);
},
});//end of ajax call
}//end of if
}); });
EDIT3 在下面的代码中我没有错误,但请求中没有任何文件。您可以在函数下方看到控制器代码。但是当我返回 true 或 false 时,文件被放置在 Request 对象中并发送到控制器。但它取代了我在退货时的所有内容。这里有什么问题?
$('form').submit(function (event) {
if ($(this).valid()) {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
$('#trainingFileDiv').html(result);
},
complete: function () {
console.log("Complete");
},
error: function () {
console.log("Error");
}
}); //end of ajax call
} //end of if
//return true;//if commented no error on jquery side but no files are passed to controlller
});
foreach (string upload in Request.Files)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
string filename = Path.GetFileName(Request.Files[upload].FileName);
TrainingFile file = new TrainingFile();
file.Name = filename;
file.Path = path;
var training = _work.TrainingRepository.GetSet().Where(a => a.EmployeeId == employeeId && a.Id == trainId).ToList().ElementAt(0);
training.TrainingFile = file;
training.FileId = file.Id;
_work.TrainingFileRepository.Add(file);
_work.TrainingFileRepository.Save();
_work.TrainingRepository.UpdateAndSave(training);
_work.TrainingRepository.Save();
Request.Files[upload].SaveAs(Path.Combine(path, filename));
}
【问题讨论】:
-
我想用返回的数据更新所有表,因为我返回相同的部分视图,结果内容将与更新版本相同。文件 div 是什么意思?
-
你添加了对 jQuery 的引用吗?例如 这个参考
-
是的,我是在母版页中完成的,我的其他部分视图正在使用 jquery,它们工作正常
标签: jquery asp.net-mvc-4 asp.net-ajax