Help with structure for “single-page” Ajax website

感情迁移 提交于 2019-12-02 09:37:52

I would suggest you leverage the MVC Framework for this. You can then easily call Controller's Action methods via client-side AJAX calls. Here's a simplified example:

// jQuery AJAX call.
function getContacts() {
    $.ajax({
        type: 'get',
        url: '/Contacts/GetContacts',
        dataType: 'json',
        success: function (response) {
            var contacts = response;
        },
        error: function (e) {
            alert('oops!');
        }
    });
}


// Server-side.
public class ContactsController : Controller
{
    [HttpGet]
    public JsonResult GetContacts()
    {
        JsonResult result = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        List<Contacts> contacts = DataAccess.GetContacts();
        result.Data = contacts;

        return result;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!