我想通过ajax请求返回一条记录
C#代码如下:
[HttpPost]
public WorkoutsViewModel GetSavedWorkoutTemplate(int WorkoutId)
{
return _db.Workouts.Where(w => w.Id == WorkoutId).Select(w => new WorkoutsViewModel
{
Tag = w.Tags.FirstOrDefault(),
Name = w.Name,
MaxEffort = w.MaxEffort,
Id = w.Id,
Description = w.Description,
Compare = w.Compare,
Category = w.Category
}).FirstOrDefault();
}
jQuery代码是:
function GetSavedWorkoutTemplate(obj) {
$("#Workout").hide();
var WorkoutId = $(obj).val();
if (WorkoutId == "") {
return;
}
var dto = { "WorkoutId": WorkoutId };
$.ajax({
type: "post",
url: "/MemberWorkout/GetSavedWorkoutTemplate",
contenttype: "application/json; charset=utf-8",
data: dto,
success: function (result) {
debugger;
$("#Workout").show();
},
error: function (result) {
alert("Unable to update status.");
}
});
}
html:
@Html.DropDownList("ddlWorkout", new SelectList(ViewBag.ddlWorkout, "Id", "Name"), "--Select Workout--", new { required = true, onchange = "GetSavedWorkoutTemplate(this);", style = "width:310px" })
在成功函数中,我得到的结果是“TheGYM.Web.Models.WorkoutsViewModel”作为字符串而不是模型,请纠正我做错了什么,我想返回一个模型。