【发布时间】:2017-03-17 19:51:31
【问题描述】:
我正在尝试传递 viewmodel 数据以在 web api 中调用..
这里是视图模型属性
public class FilterViewModel
{
public int RegionID { get; set; }
public int StateID { get; set; }
public int DistrictID { get; set; }
public int UniversityID { get; set; }
public int CollegeID { get; set; }
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
}
这是我正在尝试的 ajaxcall ..
将表单数据转换为json对象
function toSimpleJson() {
});
return json;
}
the ajax call
function GetFilteredRecords() {
var filterOptions = toSimpleJson();
$.ajax({
url: '/api/workassign',
data: JSON.stringify(filterOptions),
cache: false,
type: 'GET',
dataType: 'JSON',
success: function (data) {
debugger
}
});
}
这是过滤选项数据
这里是 api 控制器获取操作
public IEnumerable<WorkAssignViewModel> Get([FromUri]FilterViewModel date)
{
}
在这里,我将表单数据放入 json 对象并通过使用建议的 json.stringify() 并在控制器中使用 [FROMUri] 传递给控制器,但值仍然为空......
请给我一个解决方案来克服
谢谢你..
【问题讨论】:
-
filterOptions的值是什么,FilterViewModel的属性是什么? (注意设置contentType选项是没有意义的 - 它是一个 GET 并且没有正文) -
@StephenMuecke 感谢您的回复,我添加了属性和过滤选项数据..
-
只需使用
data: filterOptions,(并且由于您似乎已经正确生成了视图(至少基于您所展示的少量内容,您可以删除您的toSimpleJson()函数并使用data: $('form').serialize(), -
@StephenMuecke 删除 json.stringify() 后工作正常.. 非常感谢..
标签: ajax asp.net-mvc api asp.net-web-api