【发布时间】:2020-05-30 16:37:35
【问题描述】:
我有一个数组,它作为数字(某些标签的 ID),当这个数组传递给控制器操作方法时,我使用 ajax 调用将此数组传递给操作方法( RawTagCreation),然后这个数组中的数字用于从数据库中获取数据,然后传递给另一个 ViewModel 对象(rawTagVMs),然后我返回带有这个 ViewModel 对象的部分视图。问题是部分视图没有显示?
基本上ajax调用的成功事件也不会触发。
function selectedValues() {
var datas = [];
$('#search_to option').each(function () {
datas.push($(this).attr('value'));
});
console.log(datas);
if (datas.length > 0) {
$.ajax({
url: "@Url.Action("RawTagCreation", "SourceTagMapping")",
type: 'POST',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({ 'selectedTags': datas }),
success: function (data) {
debugger;
$('#myModalContent').html(data);
$('#myModal').modal('show');
}
});
}
else {
alert('Please Select One or More Raw Tags')
}
}
这里给出动作方法
public ActionResult RawTagCreation(int[] selectedTags)
{
List<RawTagVM> rawTagVMs = new List<RawTagVM>();
if (ModelState.IsValid)
{
foreach (var item in selectedTags)
{
var OldSourecTag = db.Real_Raw_Points.Where(x => x.Source_Tag_Id_Fk == item).FirstOrDefault();
var OPCTag = db.OPC_SourceTags.Where(x => x.Source_Tag_Id == item).FirstOrDefault();
if (OldSourecTag == null)
{
RawTagVM objToInsertRawTags = new RawTagVM();
objToInsertRawTags.Source_Tag_Id_Fk = item;
objToInsertRawTags.R_Tag_Name = "Provide Tag Name";
rawTagVMs.Add(objToInsertRawTags);
}
}
return PartialView("_Update_TagNames", rawTagVMs);
}
return View();
}
部分视图在这里
@model List<OmniConnect.ViewModel.RawTagViews.RawTagVM>
<div class="modal-header">
<h3 class="modal-title" id="exampleModalLabel">Update The Raw Tag Names</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onclick="Confirm()">Confirm Update</button>
</div>
【问题讨论】:
-
在动作中设置断点,看看会发生什么。
-
在 ajax 调用中添加一个错误并打印出错误中给出的所有信息。
-
@Neil 你能告诉我如何在错误事件中打印错误吗?
-
@nado1122 看起来您正在为您的方法安装调试器?但是在返回时,您没有接到任何电话。您可以尝试从 ajax 调用中删除 contentType: 'application/json' 吗?并添加 error: function(xhr, status,p3,p4) 并让我知道您的观察结果。
标签: c# jquery ajax asp.net-mvc