0

I want to show data which is found in a specific date range according to user inputs startdate and enddate in a chart by using MVC. To achieve this, I need json data. I can get json data according to user input but the chart cannot be seen because I could not use url:Machines/Parameter.

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 (View for Parameter Action which contains chart info)

<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>
3
  • i cant understand ur question sorry.. Commented Feb 13, 2013 at 14:13
  • 1
    You don't need both using and Html.EndForm(). Not sure if this is related to your problem or not, but the using block will implicitly close your form at the ending brace. You only need to explicitly call EndForm() if you didn't put your BeginForm() in a using block. Commented Feb 13, 2013 at 14:15
  • @GalacticCowboy Thanks for your reply, I tried what you said,but it did not work. Maybe the source of the problem is in this block I am not sure Commented Feb 14, 2013 at 7:42

1 Answer 1

1

Assuming you're using Razor syntax, replace "Machines/Parameter" by "@Url.Action("Parameter", "Machines")"

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks ken2k but I could not use this in kendo url property, it gave syntax error.
@pln What do you mean by "it does not work"? Is your controller action executed? Have you checked which URL is targeted by your request?
My controller action is working fine and return json data successfully but I could not use this converted data in a view to show a chart

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.