方法1: 使用jQuery Ajax Get调用( 部分页面更新 )。
适用于需要从数据库检索jSon数据的情况。
管制员的行动方法
[HttpGet]public ActionResult Foo(string id){ var person = Something.GetPersonByID(id); return Json(person, JsonRequestBehavior.AllowGet);}jQuery GET
function getPerson(id) { $.ajax({ url: '@Url.Action("Foo", "SomeController")', type: 'GET', dataType: 'json', // 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); } });}人类
public class Person{ public string FirstName { get; set; } public string LastName { get; set; }}方法2: 使用jQuery Ajax Post调用( 部分页面更新 )。
适用于需要将部分页面数据发布到数据库中的情况。
post方法也与上面相同,只是将
[HttpPost]Action方法替换
post为jquery方法并键入。
有关更多信息,请单击
此处将JSON数据发布到MVC控制器。
方法3: 作为表单发布方案( 整页更新 )。
适用于需要将数据保存或更新到数据库中的情况。
视图
@using (Html.BeginForm("SaveData","ControllerName", FormMethod.Post)){ @Html.TextBoxFor(model => m.Text) <input type="submit" value="Save" />}行动方法
[HttpPost]public ActionResult SaveData(FormCollection form) { // Get movie to update return View(); }方法4: 作为表单获取方案( 整页更新 )。
适用于需要从数据库获取数据的情况
获取方法也与上述相同,只是将
[HttpGet]Action方法替换
FormMethod.Get为View的form方法。
希望对您有帮助。



