如何在HttpPost

时间:2018-03-21 05:51:57

标签: c# json asp.net-mvc

我希望能够在按钮点击事件的视图上显示ViewBag,这是我的代码:

[HttpPost]
public ActionResult SpecificWorkflowReport(Report2ListViewModel wf)
{

  var getSpRecord = db.Mworkflow().ToList();

  var getRecord = (from u in getSpRecord
   select new Report2ListViewModel
      {
        WorkFlowType = u.WorkFlowType,
        WorkflowInstanceId = u.WorkflowInst,
        WorkFlowDescription = u.WorkFlowDesc,
      }).ToList();

      ViewBag.WorkflowType = wf.WorkFlowType;
      ViewBag.WorkflowInstanceId = wf.WorkflowInst;
      ViewBag.WorkFlowDescription = wf.WorkFlowDesc

      var data = Newtonsoft.Json.JsonConvert.SerializeObject(getRecord);
      return Json(data);
}

我试过这个:

Worflow Type: @ViewBag.WorkflowType
Workflow Instance Id: @ViewBag.WorkflowInstanceId
Workflow Description: @ViewBag.WorkFlowDescription

我的Javascript和json致电:

<script type="text/javascript">
$(function () {
    var table = $("#reportTable").DataTable();
        var url = $("#frmSpecificWorkflowReport").attr('action');
        var str = $("#frmSpecificWorkflowReport").serialize();
        $.ajax({
            url: url,
            type: "POST",
            data: str,
            cache: false,
            dataType: "json",
            success: function (_data) {

                if (_data.f !== undefined) {
                    swal({
                        title: "Empty Result Set",
                        text: "No record found",
                        type: "info"
                    });
                    table.clear();
                    return false;
                }

                var arr = $.map(JSON.parse(_data), function (el) { return el 
            });
                if (arr.length === 0) {
                    swal({
                        title: "Empty Result Set",
                        text: "No record found",
                        type: "info"
                    });
                }
                table.clear();
                table.destroy();
                $('#reportTable').dataTable({
                    data: arr,
                    columns: [
                        { "data": "WorkFlowType" },
                        { "data": "WorkflowInstanceId" },
                        { "data": "WorkFlowDescription" },

                    ],
                    dom: 'Bfrtip',
                    buttons: [
                        'copy', 'csv', 'excel',
                        {
                            extend: 'pdfHtml5',
                            orientation: 'landscape',
                            pageSize: 'LEGAL'
                        }

                    ]
                });
                table = $("#reportTable").DataTable();

但ViewBag值在视图上始终为null,任何帮助都将受到赞赏。我刚刚在帖子中添加了Javascript和json调用,我希望能够检索我存储的数据并将其显示在视图的任何位置

2 个答案:

答案 0 :(得分:0)

@UwakPeter您的代码片段没问题,但是您正在返回Json,可能是您通过javascript调用此方法,因此视图未更新,您需要重新加载视图,由提交按钮。

如果您使用的是javascript,则可以将数据列表和模型数据作为匿名对象传递,这样您就不需要使用ViewBag。在客户端通过ajax成功函数,你可以抓住它们( WorkflowType,WorkflowInstanceId,WorkFlowDescription,Result

[HttpPost]
public ActionResult SpecificWorkflowReport(Report2ListViewModel wf)
{
    var getSpRecord = db.Mworkflow().ToList();

    var getRecord = (from u in getSpRecord
    select new Report2ListViewModel
    {
        WorkFlowType = u.WorkFlowType,
        WorkflowInstanceId = u.WorkflowInst,
        WorkFlowDescription = u.WorkFlowDesc,
    }).ToList();

    var data = Newtonsoft.Json.JsonConvert.SerializeObject(getRecord);
    return Json(new{
        WorkflowType = wf.WorkFlowType,
        WorkflowInstanceId = wf.WorkflowInst,
        WorkFlowDescription = wf.WorkFlowDesc,
        Result= data 
    }, JsonRequestBehaviour.AllowGet);
} 

<强> JS

$.ajax({
        url: url,
        type: "POST",
        data: str,
        cache: false,
        dataType: "json",
        success: function (_data) {
            var workflowType=_data.WorkflowType; //set it to HTML control
            var workflowInstanceId =_data.WorkflowInstanceId;
            var workFlowDescription  = _data.WorkFlowDescription;
            $('#reportTable').dataTable({
                data: _data.Result
            });
        }

)};

答案 1 :(得分:-1)

试试这个,

@{  
    Layout = null;  
    ProjectMVC.Models.Record record= (ProjectMVC.Models.Record)ViewBag.Recorddetails;
}
...
Worflow Type: @record.WorkflowType