Из сервера получаем: [{"Name":"Andrew","Surname":"Romanuk","Group":"1CS-16B"}]
В представлении ( .html ):<script type="text/javascript">
$(document).ready(function () {
$("#ourBtn").click(function () {
var name = $("#Name").val();
$.ajax({
type: 'GET',
dataType: 'json',
contentType:'application/JSON',
data: { Name: name},
url: '@Url.Action("SearchResult", "Home")',
success: function (data) {
alert(data); // [{"Name":"Andrew","Surname":"Romanuk","Group":"1CS-16B"}]
var student = jQuery.parseJSON(data);
alert(student.Name); //undefined
alert(student.Surname);//undefined
//(Названия свойств совпадает с полями в модели )
}
});
});
});
</script>
<form>
<input type="text" id="Name" name="Name" />
<input type="button" id="ourBtn" name="ourBtn" value="Отправить" />
</form>
В методе:[HttpGet]
public ActionResult SearchResult(string Name)
{
var result = OurDb.Students.Where(p => p.Name == Name)
.Select(p => new { Name = p.Name, Surname=p.Surname, Group = p.Group.NameOfGroup }).ToList();
var jsonUser = JsonConvert.SerializeObject(result);
return Json(jsonUser, JsonRequestBehavior.AllowGet);
}