title: ajax post does not return view data
body: <p>I am using ajax post to call controller method <code>DeviceData()</code>. It does not return view data.<br />
  When I try to debug the code ,values are assigned to <code>viewBag</code> both in controller.cs and .cshtml but browser displays no data. Code does not return any error. After ajax call it only shows <code>patientDDL</code>.</p>
  <p>I need to display &quot;deviceName date  etc..&quot; which I assigned in <code>viewBag</code>.</p>
  <p>This is my code:</p>
  <h3><code>DeviceData.cshtml</code></h3>
  <pre><code>@model Hospital.Models.DeviceModel
  &lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;col-md-12&quot;&gt;
      &lt;div id=&quot;pdf&quot; class=&quot;pull-left&quot;&gt;
        &lt;table&gt;
          &lt;tr&gt;
            &lt;td class=&quot;blue-bgcolor&quot;&gt;Patient List : &lt;/td&gt;
            &lt;td&gt;
              @Html.DropDownList(&quot;FromJson&quot;, new SelectList(Enumerable.Empty &lt;SelectListItem&gt;()),&quot;select&quot;, 
                      new { Class = &quot;form-control&quot;, onchange = &quot;SelectedIndexChanged()&quot; })
            &lt;/td&gt;
          &lt;/tr&gt;
        &lt;/table&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;row&quot;&gt;
    &lt;table class=&quot;table valign-middle&quot;&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;Device Name&lt;/td&gt;
          &lt;td&gt;@ViewBag.deviceName&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td&gt;DateTime&lt;/td&gt;
          &lt;td&gt;@ViewBag.date&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td&gt;SPO2&lt;/td&gt;
          &lt;td&gt;@ViewBag.spo2&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td&gt;PR&lt;/td&gt;
          &lt;td&gt;@ViewBag.pr&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
  &lt;script type=&quot;text/javascript&quot;&gt;
    $(document).ready(function() {
      $.ajax({
          url: &quot;PatinetDDl&quot;,
          type: &quot;GET&quot;,
          contentType: &quot;application/json; charset=utf-8&quot;,
          datatype: JSON,
          success: function(result) {
              $(result).each(function() {
                  $(&quot;#FromJson&quot;).append($(&quot;&lt;option&gt;&lt;/option&gt;&quot;).val(this.Value).html(this.Text));
              });
          },
          error: function(data) {}
      });
    });
  &lt;/script&gt;
  &lt;script type=&quot;text/javascript&quot;&gt;
    function SelectedIndexChanged() {
        var pid = $(&quot;#FromJson&quot;).val();
        alert(pid);
        $.ajax({
            url: '/Data/DeviceData',
            type: 'POST',
            datatype: 'json',
            //contentType: 'application/json',
            data: { pid: +pid },
            success: function (result) { },
            error: function () { alert(&quot;Whooaaa! Something went wrong..&quot;) },
      });
    }
  &lt;/script&gt;
  </code></pre>
  <h3><code>Datacontroller.cs</code></h3>
  <pre class="lang-cs prettyprint-override"><code>public JsonResult PatinetDDl()
  {
      Common.DBConnect.fnconchk(con);
      DataTable dtpatient = new DataTable();
      string query = &quot;&quot;;
      query = &quot;select Preg_id,P_Name from Patient_Reg&quot;;
      SqlCommand cmd2 = new SqlCommand(query, con);
      SqlDataAdapter da = new SqlDataAdapter(cmd2);
      da.Fill(dtpatient);
  
      List&lt;SelectListItem&gt; ObjList = new List&lt;SelectListItem&gt;();
  
      foreach (DataRow row in dtpatient.Rows)
      {
          ObjList.Add(new SelectListItem()
          {
              Text = row[&quot;P_Name&quot;].ToString(),
              Value = row[&quot;Preg_id&quot;].ToString()
          });
      }
      var jsonData = ObjList;
      return Json(jsonData, JsonRequestBehavior.AllowGet);
  }
  
  
  [HttpGet]
  public ActionResult DeviceData()
  {
      DeviceModel obj = new DeviceModel();
      return View(obj);
  }
  
  [HttpPost]
  public ActionResult DeviceData(string pid)
  {
      DeviceModel obj = new DeviceModel();
      DataTable dt = new DataTable();
      Common.DBConnect.fnconchk(con);
      if (pid != null)
      {
          string query = &quot;&quot;;
          query = &quot; select D_Name,Date_Time,D_Value1 from readings where pid='&quot; + pid;
          SqlCommand cmd2 = new SqlCommand(query, con);
          SqlDataAdapter da = new SqlDataAdapter(cmd2);
          da.Fill(dt);
          ViewBag.deviceName = dt.Rows[0][&quot;D_Name&quot;].ToString();
          ViewBag.date = dt.Rows[0][&quot;Date_Time&quot;].ToString();
          ViewBag.spo2 = dt.Rows[0][&quot;D_Value1&quot;].ToString();
          ViewBag.pr = dt.Rows[0][&quot;D_Value2&quot;].ToString();
      }
      return View(obj);
  }
  </code></pre>