Click here to Skip to main content
15,896,207 members

passing controller jsonresult data to view

pln474 asked:

Open original thread
Hi,
I need to simply passed the json data which is created in controller to view (in a text area)
my codes are as follow:
My Model: Machines.cs
C#
public class MachinesSql
   {

  public List<machines> SqlAccessParameter(DateTime startDate, DateTime 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 = 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;
        }
}
My controller: MachinesController.cs
<pre lang="xml">public JsonResult Parameter(DateTime start, DateTime end)
       {
           MachinesSql a = new MachinesSql();

           var data = a.SqlAccessParameter(start, end);
           List<Machines> list = new List<Machines>(data);
         return Json(list);
       }//here I can get json data like:
[{"AutoKey":1,"MachineGroup":"Atyc","StartDate":"\/Date(1322002860000)\/","EndDate":"\/Date(1322027095000)\/","Duration":24235},{"AutoKey":2,"MachineGroup":"Fongs","StartDate":"\/Date(1322003160000)\/","EndDate":"\/Date(1322012194000)\/","Duration":9034},{"AutoKey":3,"MachineGroup":"Then","StartDate":"\/Date(1322005320000)\/","EndDate":"\/Date(1322019001000)\/","Duration":13681},{"AutoKey":4,"MachineGroup":"Numune","StartDate":"\/Date(1322008920000)\/","EndDate":"\/Date(1322031974000)\/","Duration":23053},{"AutoKey":5,"MachineGroup":"Then","StartDate":"\/Date(1322009760000)\/","EndDate":"\/Date(1322032624000)\/","Duration":22863},{"AutoKey":6,"MachineGroup":"Fongs","StartDate":"\/Date(1322010060000)\/","EndDate":"\/Date(1322067224000)\/","Duration":57163},{"AutoKey":7,"MachineGroup":"Fongs","StartDate":"\/Date(1322012220000)\/","EndDate":"\/Date(1322070357000)\/","Duration":58137},{"AutoKey":8,"MachineGroup":"Fongs","StartDate":"\/Date(1322012940000)\/","EndDate":"\/Date(1322026274000)\/","Duration":13334},{"AutoKey":9,"MachineGroup":"Then","StartDate":"\/Date(1322021640000)\/","EndDate":"\/Date(1322040622000)\/","Duration":18982},{"AutoKey":10,"MachineGroup":"Atyc","StartDate":"\/Date(1322023440000)\/","EndDate":"\/Date(1322032891000)\/","Duration":9450}]{
My View:
@{
    ViewBag.Title = "Index";
   
}
   @using (Ajax.BeginForm("Parameter", "Machines", new AjaxOptions { UpdateTargetId = "text" }))    
{
   
<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>     
<body> 
  
    @*<div id="chart"></div>*@
    <div>
       
       Start Date:  <input type="datetime" id="start" name="start" />//start parameter related with  public JsonResult Parameter(DateTime start, DateTime end)

       End Date: <input type="datetime" id="end" name="end" />//start parameter related with  public JsonResult Parameter(DateTime start, DateTime end)
 
    </div>
     <input type="submit" value="OK" />
    
   <textarea id="text"></textarea>


I am waiting for ajax.beginform updateTargetId='text' updates textarea with id='text' but it could not update.How can I use result from controller in view??
any help would be appreciated.Thanks...
Tags: MVC, ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900