我在我的網站上的購物車上有一個奇怪的問題。我收到此錯誤:MVC錯誤預計腳本部分整數返回值
Error in: https://blah.com/ShoppingCart/GetAvailability Error Message: Section not defined: "Scripts". Stack Trace: at System.Web.WebPages.WebPageBase.RenderSection(String name, Boolean required) at System.Web.WebPages.WebPageBase.RenderSection(String name)
的問題是,它無法找到返回一個整數,而不是頁面的「腳本」部分的控制器方法。其計算方法如下:
[HttpPost]
public int GetAvailability(string sku)
{
ECCDestinationConfig cfg = new ECCDestinationConfig();
try
{
RfcDestinationManager.RegisterDestinationConfiguration(cfg);
... Calculate the stock quantity from SAP
return runningTotal;
}
catch
{
// make sure the destination is unregistered
RfcDestinationManager.UnregisterDestinationConfiguration(cfg);
return 0;
}
}
控制器方法是通過AJAX稱爲像這樣:
$.ajax({
url: "/ShoppingCart/GetAvailability",
type: "POST",
data: JSON.stringify({ 'sku': '@Model.Product.SKU' }),
dataType: "json",
traditional: true,
contentType: "application/json; charset=utf-8",
success: function (data) {
//Code to set a span to show a green check if over 0 and a red
//exclamation if under.
},
error: function() {
$('#@id').addClass("fg-crimson fa fa-exclamation");
}
});
因此,該方法應該永遠只返回一個整數,但我有用戶報告的問題,我看到錯誤日誌中的「腳本」未定義錯誤消息。我無法在調試時重現這一點。我不明白爲什麼服務器在這裏期待一個頁面。奇怪的是,如果我重置網站的應用程序池,問題將暫時消失。我顯然不會在某處陷入一些錯誤,但我無法想象可能會發生什麼。
ajax頁面是否有表單,並且您是否阻止表單的默認提交? – Jasen
@Jasen ajax調用實際上來自表中的局部視圖。該部分顯示一個加載圖標,直到獲得結果,此時它顯示一個複選標記或感嘆號。每頁通常有多個呼叫,因爲它是按行項目。 – Nick