C# Jquery/ajax从类中获取对象,并在我的视图中的文本框中使用它

C# Jquery/ajax从类中获取对象,并在我的视图中的文本框中使用它,c#,asp.net-mvc-3,jquery,C#,Asp.net Mvc 3,Jquery,我正在开发一个asp.net mv3应用程序 在一个helper类中,我有一个方法,它根据一个人的ID返回一个对象 public Person GetPersonByID(string id) { // I get the person from a list with the given ID and return it } 在视图中,我需要创建一个jquery或javascript函数,该函数可以调用GetPersonByID function getPerson(id) {

我正在开发一个asp.net mv3应用程序

在一个helper类中,我有一个方法,它根据一个人的ID返回一个对象

public Person GetPersonByID(string id)
{
    // I get the person from a list with the given ID and return it
}
在视图中,我需要创建一个jquery或javascript函数,该函数可以调用
GetPersonByID

function getPerson(id) {
    //I need to get the person object by calling the GetPersonByID from the C# 
    //helper class and put the values Person.FirstName and Person.LastName in 
    //Textboxes on my page
}
我该怎么做

这可以通过使用和ajax调用来实现吗

    $.ajax({
            type:
            url:
            success:
            }
        });
非常感谢您的帮助


谢谢你,Javascript或jQuery不知道
方法是什么意思。jQuery不知道C是什么。jQuery不知道什么是ASP.NETMVC。jQuery不知道
Person
.NET类的意思。jQuery不知道.NET类是什么意思

jQuery是一个javascript框架,它(以及其他许多东西)可以用来向服务器端脚本发送AJAX请求

在ASP.NET MVC中,这些服务器端脚本称为控制器操作。在Java中,这些被称为servlet。在PHP-PHP脚本中。等等

因此,您可以编写一个控制器操作,该操作可以使用AJAX进行查询,并将返回
Person
类的JSON序列化实例:

public ActionResult Foo(string id)
{
    var person = Something.GetPersonByID(id);
    return Json(person, JsonRequestBehavior.AllowGet);
}
然后:

function getPerson(id) {
    $.ajax({
        url: '@Url.Action("Foo", "SomeController")',
        type: 'GET',
        // we set cache: false because GET requests are often cached by browsers
        // IE is particularly aggressive in that respect
        cache: false,
        data: { id: id },
        success: function(person) {
            $('#FirstName').val(person.FirstName);
            $('#LastName').val(person.LastName);
        }
    });
}
这显然假设Person类具有
FirstName
LastName
属性:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

当然可以!在服务器端代码中,特别是在控制器中,只需返回序列化为JSON对象的Person,如下所示:

[HttpPost] 公共操作结果GetPersonByID(字符串id) { 返回Json(人); }

然后在AJAX中

        $.ajax({
            type: "POST",
            url: form.attr('action'),
            data: form.serialize(),
            error: function (xhr, status, error) {
                //...
            },
            success: function (result) {
                // result.FirstName
            }
        });

我正在努力学习这一点,因为我正在做一些非常相似的事情。在
$.ajax
中,
id
字符串的值设置在哪里?这就是传递到
ActionResult
方法中的内容,对吗?另外,如果我的ActionResult方法没有或不需要输入参数,该怎么办?