我使用此代碼訪問我的服務器(MVC),這工作正常。在結果「數據」({「Id」:30,「說明」:「樣品照片」,「名稱」:「第一Galery」})我嘗試獲取屬性data.Name,這只是沒有返回,什麼是在這個代碼中的問題?Json的結果是沒有什麼
的JavaScript
$(function() {
$('#UserGaleries_').change(function() {
try {
if ($(this).val() == -1) {
$('#NameGaleriesEdit').val('');
$('#DescriptionGaleriesEdit').val('');
}
else {
$.post('/UserGaleries/ChangeCategorie',
{ selectedID: $(this).val() },
function (data) {
alert(data.Name); //Nothing
$('#NameGaleriesEdit').val(data.name);
$('#DescriptionGaleriesEdit').val('asdf');
});
}
} catch (e) {
alert(e);
}
});
});
MVC
[Serializable]
public class ResponsetModel
{
public int Id { get; set; }
public string Description { get; set; }
public string Name { get; set; }
}
public JsonResult ChangeCategorie(int selectedID)
{
DbLayer.UserGaleriesManager uc = new DbLayer.UserGaleriesManager();
DbLayer.Models.UsersGalery cat = uc.GetGaleriesById(selectedID);
ResponsetModel retValue = new ResponsetModel()
{ Id = cat.Id, Name = cat.Title, Description = cat.Description };
JsonResult oView = Json(retValue, "text/plain", System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet);
return oView;
}
您的ChangeCategories操作方法需要HttpPost屬性 – Jon 2012-02-19 20:36:33
@Jon。這是不正確的。 – gdoron 2012-02-19 20:39:12
不知道JavaScript的奧祕,但你有沒有嘗試調用data().name而不是? – 2012-02-19 20:39:36