私はこのコードを使用して私のサーバー(MVC)にアクセスしています。結果 "data"({"Id":30、 "Description": "サンプルPhotos"、 "Name": "First Galery"})プロパティのデータを取得しようとしました。このコードの問題?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
@Jonが必要です。それは間違っている。 – gdoron
JavaScriptの謎を知りませんが、代わりにdata()。nameを呼び出してみましたか? –