ajax - JSON with MVC is not working -


<div id="updatepanel"> </div> 

my js script:

<script type="text/javascript">              $("#mydropdownid").change(function () {                 $.ajax({                     url: "@url.action("mymethodtogetlist","mycontroller")",                     contenttype: "application/json; charset=utf-8",                     type: 'get',                     datatype: 'json',                     success: function (data) {                         loaddata(data);                     },                     error: function (xhr, status, error) {                         alert(error);                     }                 });             });              function loaddata(data) {                 var tab = $('<table class="mytable"></table>');                 var thead = $('<thead></thead>');                 thead.append('<th>name</th>');                  tab.append(thead);                 $.each(data, function (i, val) {                     var trow = $('<tr></tr>');                     trow.append('<td>' + val.name + '</td>');                     tab.append(trow);                 });                 $("tr:odd", tab).css('background-color', '#c4c4c4');                 $("#updatepanel").html(tab);             };         });     </script> 

controller:

[webmethod] public jsonresult mymethodtogetlist() {     list<t> list = class1.returnlistwiththisparam(1);      return json(list, jsonrequestbehavior.allowget); } 

i'm trying list<> using json,..

i'd set breakpoint in method, , works, retrieves 1 element, in browser got fail alert, why? doing wrong?

other way (not works too):

<script type="text/javascript">         $('#mydropdownid').change(function () {             $.getjson('@url.action("mymethodtogetlist", "mycontroller")', function(result) {                 var ddl = $('#mydropdownid');                 ddl.empty();                 $(result).each(function() {                     ddl.append(                     $('<option />', {                         value: this.id                     }).html(this.name)                     );                 });             });     </script> 

i trying populate dropdown json:

@html.dropdownlistfor(x => x.myproperty, new selectlist(enumerable.empty<selectlistitem>())) 

my model:

 [metadatatype(typeof(sellermetadata))]  public partial class seller  {        public seller()     {         this.salerecords = new hashset<salerecord>();     }      public int id { get; set; }     public string name { get; set; }     public string cpf { get; set; }     public string rg { get; set; }     public datetime admissiondate { get; set; }     public situation situation { get; set; }     public scholarity scholarity { get; set; }     public int deptid { get; set; }      public virtual department department { get; set; }     public virtual icollection<salerecord> salerecords { get; set; } } 

i examined in browser, , got error:

system.web.mvc.controller.endexecutecore(iasyncresult asyncresult)    em system.web.mvc.controller.&lt;beginexecute&gt;b__15(iasyncresult asyncresult, controller controller)    em system.web.mvc.async.asyncresultwrapper.wrappedasyncvoid`1.callenddelegate(iasyncresult asyncresult)    em system.web.mvc.async.asyncresultwrapper.wrappedasyncresultbase`1.end()    em system.web.mvc.async.asyncresultwrapper.end[tresult](iasyncresult asyncresult, object tag)    em system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag)    em system.web.mvc.controller.endexecute(iasyncresult asyncresult)    em system.web.mvc.controller.system.web.mvc.async.iasynccontroller.endexecute(iasyncresult asyncresult)    em system.web.mvc.mvchandler.&lt;beginprocessrequest&gt;b__5(iasyncresult asyncresult, processrequeststate innerstate)    em system.web.mvc.async.asyncresultwrapper.wrappedasyncvoid`1.callenddelegate(iasyncresult asyncresult)    em system.web.mvc.async.asyncresultwrapper.wrappedasyncresultbase`1.end()    em system.web.mvc.async.asyncresultwrapper.end[tresult](iasyncresult asyncresult, object tag)    em system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag)    em system.web.mvc.mvchandler.endprocessrequest(iasyncresult asyncresult)    em system.web.mvc.mvchandler.system.web.ihttpasynchandler.endprocessrequest(iasyncresult result)    em system.web.httpapplication.callhandlerexecutionstep.system.web.httpapplication.iexecutionstep.execute()    em system.web.httpapplication.executestep(iexecutionstep step, boolean&amp; completedsynchronously) --&gt; 

try this

     $.ajax({                         type: 'get',                         url: '/yourcontroller/yourmethod',                         contenttype: "application/json",                         datatype: "json",                         success: function(data) {                       alert('success');                         },                         error: function() {                             alert('fail');                         }                     });  public actionresult yourmethod()         {           return json("1", jsonrequestbehavior.allowget);         } 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -