我使用此代码访问我的服务器(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