C# 为什么我的局部视图没有加载正确的内容?
我有我的部分观点,其中包括C# 为什么我的局部视图没有加载正确的内容?,c#,asp.net,asp.net-mvc,razor,partial-views,C#,Asp.net,Asp.net Mvc,Razor,Partial Views,我有我的部分观点,其中包括 @model List<Destination> @for (var i = 0; i < Model.Count; i++) { @Html.HiddenFor(model => model[i].Id) <div class="form-row"> <div class="form-group col-md-10"> &
@model List<Destination>
@for (var i = 0; i < Model.Count; i++)
{
@Html.HiddenFor(model => model[i].Id)
<div class="form-row">
<div class="form-group col-md-10">
<label>@Html.LabelFor(model => model[i].Path) ( @(i + 1) )</label>
<input type="text" value=@Model[i].Path/>
@Html.TextBoxFor(model => model[i].Path, new { @class = "form-control", required = "required" , title = "My custom error message")
@Html.ValidationMessageFor(model => model[i].Path)
</div>
<button class="btn btn-outline-danger" onclick="deleteField(event,
'@Model[i].Id');">
<i class="fas fa-trash-alt"></i>
</button>
}
而JS:
function deleteField(event, clickedId) {
event.preventDefault();
var model = $("#Form").serialize();
debugger;
$.ajax({
type: 'POST',
url: '@Url.Action("SupprimerChampDestination", "PathsController")',
data: model + "&idToDelete=" + clickedId,
success: function (response) {
debugger;
console.log(response);
$("#destinationMultiple").html(response);
},
error: function(xhr, textStatus, error) {
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);
}
});
}
$(“#destinationMultiple”)是包含加载结果的局部视图的div
谢谢您能告诉我们您是如何在用例中包含局部视图的吗?使用局部视图时,需要将适当的viewmodel传递给局部视图。如果类型与部分视图中的<代码>模型代码>不匹配,则该类型将为空。@GlennFerrie编辑了答案,但部分视图接收到的模型是正确的,奇怪的是总体工作不正常。您将列表值的更改保留在哪里?与foreach相反,for循环有什么具体原因吗?@melkisadek foreach没有将验证等绑定到正确的索引,所有内容都链接到第一个元素,在程序流中,您试图从列表中删除项目的位置?显示的部分代码中没有删除项目的内容。另外,您可以发布呈现的HTML吗?您可以向我们展示如何在用例中包含部分视图吗?使用局部视图时,需要将适当的viewmodel传递给局部视图。如果类型与部分视图中的<代码>模型代码>不匹配,则该类型将为空。@GlennFerrie编辑了答案,但部分视图接收到的模型是正确的,奇怪的是总体工作不正常。您将列表值的更改保留在哪里?与foreach相反,for循环有什么具体原因吗?@melkisadek foreach没有将验证等绑定到正确的索引,所有内容都链接到第一个元素,在程序流中,您试图从列表中删除项目的位置?显示的部分代码中没有删除项目的内容。另外,您可以发布呈现的HTML吗?
function deleteField(event, clickedId) {
event.preventDefault();
var model = $("#Form").serialize();
debugger;
$.ajax({
type: 'POST',
url: '@Url.Action("SupprimerChampDestination", "PathsController")',
data: model + "&idToDelete=" + clickedId,
success: function (response) {
debugger;
console.log(response);
$("#destinationMultiple").html(response);
},
error: function(xhr, textStatus, error) {
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);
}
});
}