ASP.NET MVC中从后台控制器(Controller)传递数据到前台页面视图(View)方式

方式一:

数据存储模型Model:

public class CalendarEvent
    {
        public string id { get; set; }
        public DateTime start { get; set; }
        public DateTime end { get; set; }
        public string backgroundColor { get; set; }
        public string title { get; set; }
        public string allDay { get; set; }
    }

View Code

前台接收显示数据视图View:

<script type="text/javascript">
        $(function () {
            //calendar handle data as follows:

            var events = [];
            $.ajax({
                url: "/DeploymentTask/CalendarData",
                success: function (data) {
                    events = data;
                },
                async: false
            });

            $("#calendar").fullCalendar({

                header: {
                    left: 'prev,next today',
                    center: 'title',
                    //right: 'month,agendaWeek,agendaDay'
                    right: 'month'
                },
                selectable: true,
                weekMode: 'variable',//fixed,variable,liquid
                events: events,
                defaultEventMinutes: 1440  //默认事件长度为一天
            });
            
        });
    </script>

View Code

后台处理数据控制器Controller:

public class DeploymentTaskController : Controller
    {
        public JsonResult CalendarData()
        {
            Operation op = new Operation();


            List<CalendarEvent> calendarData = op.GetData();

            return Json(calendarData, JsonRequestBehavior.AllowGet);


        }
    }

View Code

传输中数据样式截图:

 

 

方式二: 

数据存储模型Model:此方式未用到Model存储,是以向前台页面视图View传递字符串string型数据(包括string型json数据),用到传递json型数据时需要建立数据存储的Model。这里使用字符串拼接方法实现的
前台接收显示数据视图View:

<script type="text/javascript">
        $(document).ready(function () {

            var INIData = [];
            
            $.ajax({
                url: '/ModifyINIFile/ShowINIFileData',
                success: function (data) {
                    INIData = data;
                },
                async:false
            });
            
            $('#tt').tree({

                method: 'get',
                data: INIData,
                checkbox: true,
                lines: true,
                animate: true,
                
                onCheck: function (node,checked) {
                    //alert(node.checked);
                    //alert(node.target);
                    changeKeyData = node.text;
                    changeValueData = node.checked;

                    changeData +="*"+changeKeyData +"|"+ changeValueData;
                    
                },
                onlyLeafCheck:true,
                cascadeCheck: false

            });
        });
</script>

View Code

后台处理数据控制器Controller:

public class ModifyINIFileController : Controller
    {
        //
        // GET: /ModifyINIFile/

        public ContentResult ShowINIFileData()
        {

            string jsonData = op.GetINIFileData();
            
            return new ContentResult { Content = jsonData, ContentType = "application/json" };

        }
    }     

View Code

传输中数据样式截图:

 

本人用到及此,下次遇到再续写,谢谢!(有错有缺陷请指导)

更多知识分享:微笑空间站

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页