我想根据用户输入startdate
和enddate
使用 MVC 在图表中显示在特定日期范围内找到的数据。为此,我需要 json 数据。我可以根据用户输入获取 json 数据,但无法看到图表,因为我无法使用url:Machines/Parameter
.
索引.cshtml
<body>
@using (Html.BeginForm("Parameter", "Machines", FormMethod.Post))
{
<div>
Start Date: <input type="datetime" id="start" name="start" />
End Date: <input type="datetime" id="end" name="end" />
</div>
<input type="submit" value="OK" />
Html.EndForm();}
机器控制器.cs
public ActionResult Index()
{
return View();
}
public ActionResult Parameter(DateTime start, DateTime end)
{
MachinesSql a = new MachinesSql();
var data = Json(a.SqlAccessParameter(start, end), JsonRequestBehavior.AllowGet);
return View(data);
}
型号:Machines.cs
namespace DenemeReport.Models
{
public class Machines
{
[Key]
public int AutoKey;
public string MachineGroup;
public DateTime StartDate;
public DateTime EndDate;
public int Duration;
}
public class MachinesDBContext : DbContext
{
public DbSet<Machines> Machine { get; set; }
}
public List<Machines> SqlAccessParameter(DateTime startDate, DateTime endDate)
{
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 =startDate;
myCommand.Parameters.Add("@SP_endDate", SqlDbType.DateTime).Value = endDate;
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;
}
Parameter.cshtml(包含图表信息的参数操作视图)
<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 id="chart"></div>
<div id="chart2"></div>
<body>
<script>
$("#chart").kendoChart
({
theme: $(document).data("kendoSkin") || "default",
title: {
text: "Pie Chart Test"
},
legend: {
position: "bottom",
},
dataSource:
{
transport:
{
read:
{
url: "Machines/Parameter", // This part create the problem I think. The url does not work.
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>
</body>