我在同一条线上尝试一些东西。由于我没有在一个地方找到完整的答案,这应该会有所帮助。
问题:如何在编辑时避免回发 | 删除视图中的行,尤其是那些由模型上的foreach循环生成的行。
以下是几个关键点:
- 确保这个视图是一个部分视图,它是其他视图的一部分(对于初学者,它类似于在“_Layout”视图中调用“_LoginPartial”的方式)并且最好放置在具有唯一 ID 的分区中
<div id="PartialView">
@Html.Partial("PartialView", Model)
</div>
- 例如,不要使用 @Html.ActionLink() 使用任何 HTML 标记:
从:
@Html.ActionLink("DisplayText", "Method", "Controller", new { Var1=
@model.element1, var2=@Model.Element})
到 :
<input type="button" class="UniqueClassName" value="Delete"
Var1="@item" var2="@Model.Element">
//控制器方法调用会在AJAX函数调用中处理
public ActionResult Method(string MethodParamName1, string MethodParamName2)
{
\\ Write your logic
\\ Construct your object to return to partial view
return View(Model);
}
<table class="table">
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item)
</td>
<td>
<input type="button" class="buttonDelete" value="Delete File" Fname="@item" ProjectName="@Model.ProjectName">
</td>
.....
</td>
</tr>
}
</table>
<html>
<head>
<title>Upload Example</title>
<script src="~/Scripts/jquery-1.10.2.intellisense.js"></script>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function () {
// $(document) in teh below line is the KEY. It tells any button click that has classname equals UniqueClassName
$(document).on('click', '.UniqueClassName', function () {
var formData = new FormData();
var FuncParam1 = $(this).attr('Var1');
var FuncParam2 = $(this).attr('Var2');
formData.append("MethodParamName1", FuncParam1 );
formData.append("MethodParamName2", FuncParam2 );
$.ajax({
data: formData,
type: "POST",
url: '/Controller/Method',
contentType: false,
processData: false,
success: function (response) {
$('#PartialView').html(response);
// Make sure the name after # is same as id
// of <div> where partila view is placed
},
error: function (error) {
alert("errror");
}
});
});
});
</script>
</head>
<body>
<div id="PartialView">
@Html.Partial("PartialView", Model)
</div>
......
......
EXTRA FEILDS IF ANY
......
......
</body>
</html>