You*_*sef 3 c# jquery asp.net-mvc-3
我正在开发一个asp.net mv3应用程序.
在一个帮助器类中,我有一个方法可以根据他的ID返回一个人的对象
public Person GetPersonByID(string id)
{
// I get the person from a list with the given ID and return it
}
Run Code Online (Sandbox Code Playgroud)
在视图中我需要创建一个可以调用的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
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
这可以通过使用和ajax调用来完成吗?
$.ajax({
type:
url:
success:
}
});
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏
谢谢
Dar*_*rov 12
关于这个问题的Javascript或jQuery不知道是什么method意思.jQuery不知道C#是什么.jQuery不知道ASP.NET MVC是什么.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);
}
Run Code Online (Sandbox Code Playgroud)
然后:
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);
}
});
}
Run Code Online (Sandbox Code Playgroud)
这显然假设您的Person类具有FirstName和LastName属性:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9696 次 |
| 最近记录: |