Send model to Jquery

落爺英雄遲暮 提交于 2019-12-22 17:51:48

问题


I would like to create tree in my mvc view using Fuelux jquery plugin. To use this plugin I have to send data to plugin with next code:

$('#MyTree').tree({ dataSource: dataSource })

I don't have any idea how to send my model object in view as parameter to plugin. What structure data must have?


回答1:


You need to conver you model to Json and use the converted object as a datasource:

<script>
$(dcoument).ready(function(){

var datasource = @ViewBag.JsonModel;
$('#MyTree').tree({ dataSource: dataSource });

});
</script>

Now you need to populate ViewBag.JsonModel in the action:

public ViewResult YourActionName()
{
 // your logic on getting model
ViewBag.JsonModel = //convert model to json using jsonserializer
}

Here is tree plugin example:

 // INITIALIZING TREE
var treeDataSource = new TreeDataSource({
data: [
{ name: 'Test Folder 1', type: 'folder', additionalParameters: { id: 'F1' } },
{ name: 'Test Folder 2', type: 'folder', additionalParameters: { id: 'F2' } },
{ name: 'Test Item 1', type: 'item', additionalParameters: { id: 'I1' } },
{ name: 'Test Item 2', type: 'item', additionalParameters: { id: 'I2' } }
],
delay: 400
});
$('#MyTree').tree({dataSource: treeDataSource}); 


来源:https://stackoverflow.com/questions/17485568/send-model-to-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!