通过控制器JSON结果查看 [英] passing controller json result to view
问题描述
我要简单地传递这是在控制器中创建,查看JSON数据
我的codeS如下:
I need to simply passed the json data which is created in controller to view my codes are as follow:
我的模型: Machines.cs
在我的模型,我得到的日期作为字符串,并将其解析为datetime。解析操作成功时,我打一个断点,我看到的结果。
My Model: Machines.cs In my model I get the dates as a string and parse them to datetime. Parsing operation is successful when I hit a breakpoint there,I see the result.
public class Machines
{
[Key]
public int AutoKey;
public string MachineGroup;
public DateTime StartDate;
public DateTime EndDate;
public int Duration;
public List<Machines> SqlAccessParameter(string startDate,string endDate)
{
DateTime sDate = DateTime.Parse(startDate);
DateTime eDate = DateTime.Parse(endDate);
string connstr = "Data Source=USER-BILGISAYAR;Initial Catalog=Report;Integrated Security=True";
SqlConnection myConnection = new SqlConnection(connstr);
myConnection.Open();
SqlCommand myCommand = new SqlCommand("DateRange", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add("@SP_startDate", SqlDbType.DateTime).Value = sDate;
myCommand.Parameters.Add("@SP_endDate", SqlDbType.DateTime).Value = eDate;
SqlDataAdapter dataAdapter = new SqlDataAdapter();
myCommand.ExecuteNonQuery();
dataAdapter.SelectCommand = myCommand;
DataSet dSet = new DataSet();
dataAdapter.Fill(dSet);
myConnection.Close();
List<Machines> machinePost = new List<Machines>();
foreach (DataRow row in dSet.Tables[0].Rows)
{
Machines mac = new Machines();
mac.AutoKey = (int)row["AUTOKEY"];
mac.MachineGroup = (string)row["MACHINEGROUP"];
mac.Duration = (int)row["DURATION"];
mac.StartDate = (DateTime)row["STARTTIME"];
mac.EndDate = (DateTime)row["ENDTIME"];
machinePost.Add(mac);
}
return machinePost;
}
}
}
我的控制器: MachinesController.cs
public ActionResult MachineParameter()
{
Machines model = new Machines();
return View("Index",model);
}
我的观点:指数
在我看来,我尽量按照那就是在用户指定的日期范围的数据绘制图表,但并不成功。
My View:Index In my view I try to draw a chart according to data that is in the user specified date range but it is not successful.
@model DenemeReport.Models.Machines
@{
ViewBag.Title = "Index";
}
<head>
<title>Intro</title>
<script src="~/Scripts/jquery-1.8.3.min.js"></script>
<script src="~/Scripts/kendo/kendo.all.min.js"></script>
<link href="~/Scripts/kendo/kendo.common.min.css" rel="stylesheet" />
<link href="~/Scripts/kendo/kendo.default.min.css" rel="stylesheet" />
</head>
<div>
Start Date: <input type="text" id="start" name="start" />
End Date: <input type="text" id="end" name="end" />
</div>
<input type="button" value="OK" onclick="CallAjaxFunction()" />
<body>
<div id="chart"></div>
<script>
function CallAjaxFunction() {
var sDate = document.getElementById('start').value;
var eDate = document.getElementById('end').value;
$.ajax
({
url: "@Url.Action("MachineParameter","Machines")",
data: { startDate: sDate, endDate: eDate },
type: "POST",
success: function () {
alert(sDate);
$("#chart").kendoChart
({
theme: $(document).data("kendoSkin") || "default",
title: {
text: "Pie Chart Test"
},
legend: {
position: "bottom",
},
dataSource:
{
transport:
{
read:
{
url: "@Url.Action("MachineParameter", "Machines")",
dataType: "json",
contentType: 'application/json; charset=utf-8'
}
}
},
seriesDefaults: {
labels: {
visible: true,
format: "{0}"
}
},
series: [{
type: "pie",
field: "Duration",
categoryField: "MachineGroup"
}],
tooltip: {
visible: true,
format: "{0}"
}
});
}
});
}
</script>
推荐答案
您可以通过您的模型访问,或使一个AJAX请求到参数
在你的控制器动作。我已经改写了您的参数操作返回查看和使用MachinesSql作为模型:
You could access it via your Model or make an AJAX request to the Parameter
action in your controller. I have rewrote your Parameter Action to return the View and use the MachinesSql as the model:
public ActionResult Parameter()
{
MachinesSql model = new MachinesSql();
return View("YourViewName", model);
}
然后在你的观点,你可以这样做:
Then in your view you could do:
@Model MachinesSql
foreach(var m in @Model.SqlAccessParameter(startDate, endDate)) {
@Html.DisplayFor(m=>m.DataValue);
}
编辑:显示如何通过Ajax调用达到
function CallAjaxFunction() {
var sDate = document.getElementById('start');
var eDate = document.getElementById('end');
$.ajax{({
url: '@Url.Action("Parameter")',
data: { startDate: sDate, endDate: eDate },
type: "POST",
success: function(content) {
alert(content);
}
});
}
这篇关于通过控制器JSON结果查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!