2013-02-13 48 views
0

我想通過使用MVC根據用戶輸入startdateenddate在圖表中顯示在特定日期範圍內找到的數據。爲了實現這一點,我需要json數據。 我可以根據用戶輸入獲取json數據,但無法看到圖表,因爲我無法使用url:Machines/ParameterMVC:在視圖中使用控制器數據

index.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();} 

MachinesController.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); 

} 

MODEL: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> 
+0

我無法理解烏爾問題抱歉.. – Neel 2013-02-13 14:13:49

+1

你並不需要所有的'using'和'Html.EndForm()'。不知道這是否與你的問題有關,但「使用」塊將隱含地關閉你的表格在結束括號處。如果您沒有將'BeginForm()'放入'using'塊中,您只需顯式調用'EndForm()'。 – GalacticCowboy 2013-02-13 14:15:36

+0

@GalacticCowboy感謝您的回覆,我嘗試了您所說的,但沒有奏效。也許問題的根源在於我不確定 – pln 2013-02-14 07:42:41

回答

1

假設你正在使用剃刀語法,替代"Machines/Parameter"通過"@Url.Action("Parameter", "Machines")"

+0

謝謝ken2k,但是我不能在kendo url屬性中使用它,它給出了語法錯誤。 – pln 2013-02-14 07:46:41

+0

我解決了語法錯誤,但它仍然不起作用 – pln 2013-02-14 08:19:04

+0

@pln你是什麼意思的「它不工作」?你的控制器動作是否被執行您是否檢查過您的請求所針對的是哪個網址? – ken2k 2013-02-14 09:08:43

相關問題