我對客戶端服務器編程概念不熟悉。我需要發送四個js vars到我的MVC 3控制器動作。如何將js變量發送給mvc控制器
$(document).ready(function() {
var $jcrop;
$('#image').Jcrop({
bgColor: 'red',
onSelect: refreshData
}, function() {
$jcrop = this;
});
function refreshData(selected) {
myData = {
x1: selected.x1,
x2: selected.x2,
y1: selected.y1,
y2: selected.y2
};
}
});
所以我在瀏覽器中得到了我的vars。
在服務器端我有是:
public ActionResult CreateCover(ImageCoordinates coordinates) {
ViewData.Model = coordinates;
return View();
}
public class ImageCoordinates
{
public int x1 { get; set; }
public int x2 { get; set; }
public int y1 { get; set; }
public int y2 { get; set; }
}
是否有一個簡單的方法來後我從JS 4個PARAMS我的行動? 我試圖用幾個例子,從這個網站上,使用$就
像這樣
$.ajax({
url: '/dashboard/createcover',
type: 'POST',
data: JSON.stringify(myData),
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert(data.success);
},
error: function() {
alert("error");
},
async: false
});
但它不工作,我在操作中收到0 0 0 0。但老實說,我甚至不知道如何調用這個jquery函數。我應該通過點擊按鈕來調用它,還是當我發佈表單時自動調用它? 還有其他方法發送這個參數嗎?
突出顯示最重要的部分「數據:JSON.stringify({coordinates:model}),」 –
謝謝你,我測試了這一點,並且我收到了控制器操作中的變量。還有一個問題是,如何用我收到的參數來呈現另一個視圖。 – CadmusX
public ActionResult CreateCover(ImageCoordinates coordinates){ViewData.Model = coordinates; return View(); }這是我的視圖代碼,但我仍然在那個頁面上發送了我的參數。在調試中,我收到了我的變量,但視圖並不呈現。 – CadmusX