當用戶點擊評論圖標時,必須進行以下操作:(a)調用駐留在customscript.js文件中的名爲SearchData(urlVal,paramVal,callback)的AJAX函數。在Movie Controller中傳遞指向MovieReview操作方法的參數,查詢的值應該是movie Id。 (b)創建一個名爲successMovieReviewHandler的函數,它是成功回調函數中的AJAX,它接收AJAX調用的結果。在這裏面,功能檢查錯誤和數據有效性。將標題分配給您的模態標題,PartialView以模態正文並使用這些設置鍵盤調用您的模式:false和背景:「靜態」?Ajax成功回調函數
這是我的代碼在我的影片控制器:
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult MovieReview(Guid? query)
{
var m = db.Movies.FirstOrDefault(x => x.Id == query);
if (m != null)
{
MovieReview review = new MovieReview();
review.Id = (Guid)query;
var viewContent = ConvertViewToString("_MovieReview", review);
//var viewContent = ConvertViewToString("_ReviewModal", review);
return Json(new { PartialView = viewContent, Title = m.Title, Success = true }, JsonRequestBehavior.AllowGet);
}
else
{
return Json("",JsonRequestBehavior.AllowGet);
}
}
[ChildActionOnly]
private string ConvertViewToString(string viewName, object model)
{
ViewData.Model = model;
using (StringWriter writer = new StringWriter())
{
var vResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var vContext = new ViewContext(ControllerContext, vResult.View, ViewData, (new TempDataDictionary()), writer);
vResult.View.Render(vContext, writer);
return writer.ToString();
}
}
這是Ajax的功能:
function SearchData(urlVal, paramVal, callback) {
$.ajax({
url: urlVal,
type: "GET",
dataType: "json",
async: true,
data: { query: paramVal },
success: callback,
error: function (data) {
// data contains the value returned by the server
console.log("Error : " + data);
}
});
}
請幫我創造了ajax成功回調函數!
這很含糊。關於回調的具體內容是否需要幫助? – Carcigenicate
無法在索引視圖中創建Ajax回調。需要將數據從控制器傳遞到局部視圖才能使用彈出編輯! –