我通过jquery或ajax向mvc操作发送json数组,action正确处理请求。但是,mvc操作返回了一个视图,我需要重定向到这个视图(或者用它替换一个主体),但是我不知道怎么做。
因此,除了返回值之外,该操作运行良好:
[HttpPost]
public ActionResult CreateGet(List<string> itemIds)
{
List<TempItem> items = new List<TempItem>();
foreach (string item in itemIds)
{
items.Add(CallApi.Get(Request.Cookies["jwt"], "tempitems", item.ToString()).Content.ReadAsAsync<TempItem>().Result);
}
Invoice inv = new Invoice()
{
IsSupplement = items[0].IsSupplement,
Date = DateTime.Now,
Employee = CallApi.Get(Request.Cookies["jwt"], "employees/getprofile").Content.ReadAsAsync<Employee>().Result,
InvoiceItems = new List<InvoiceItem>()
};
foreach(TempItem item in items)
{
inv.InvoiceItems.Add(new InvoiceItem { Amount = item.Amount, ProductId = item.ProductId, Product = item.Product });
}
return View(inv);
}
以及razor页面中的脚本,它收集选定的id并将它们发布到操作中。
在发布之后,没有其他事情发生,即使没有调用警报,即使视图页面存在并且我在控制台中没有看到失败。
function CreateInvoice(id) {
var selected = $('#' + id).DataTable().rows('.selected').data();
var items = [];
for (i = 0; i < selected.length; i++) {
items.push(selected[i][0]);
}
var postData = { itemIds: items };
$.ajax({
type: "POST",
url: "/Invoices/CreateGet",
data: postData,
success: function (data) {
alert("success");
window.location.href = data.url;
},
dataType: "json",
traditional: true
});
}
更新
好吧,我放弃了这种胡说八道,坚持获取通过url中的id数组的请求。我想我只是做错了事情。