Попробуйте использовать JsonResult в MVC, чтобы вернуть Json вместо построения строки. JsonResult - это просто класс абстракции ActionResult. Измените код в контроллере следующим
Метод 1:
public JsonResult GetTest(int id)
{
return this.Json(new { ID = 0 }, JsonRequestBehavior.AllowGet);
}
ИЛИ
Способ 2:
Пытаться создать класс модели, как LongPachage
public class LongPachage()
{
public int ID {get;set;}
}
и попытаться вызвать в метод контроллера следующим
public JsonResult Get(int id)
{
LongPachage model = new LongPachage();
model.ID = 0;
return this.Json(model, JsonRequestBehavior.AllowGet);
}
ИЛИ
Метод 3
Создать класс модели говорят TestModel и попытаться добавить класс LongPachage в Собственость этого класса.
public class TestModel()
{
public LongPachage LongPachage {get;set;}
}
и попытаться вызвать в метод контроллера следующим
public JsonResult Get(int id)
{
TestModel model = new TestModel();
model.LongPachage .ID = 0;
return this.Json(model, JsonRequestBehavior.AllowGet);
}
, а затем в окне с помощью AJAX GET обертку попытаться реализовать следующим образом
$.get('@Url.Action("Get","ControllerName")', { id: "5" })
.done(function (data) {
// If you are using Method 1 or Method 2 alert will be as follows
alert(data.ID);
// If you are using method 3
alert(data.LongPachage.ID)
});
Try 'data.longPachage. Id' –
Не пытайтесь вернуть 'json' так. Вместо этого возвратите 'JsonResult' объекта (и вы можете удалить бессмысленное' contentType: "application/json; charset = utf-8", 'option) –
это не работает –