Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This is driving me crazy. All I'm trying to do is to pass in a Id to a ActionMethod which is working and have an Object be returned to the javascript. Then in javascript, I want to be able to say something like..Objec.Property, ie/ Student.Name, or Student.GPA.

Any help is appreciated. I tried json but couldn't get that to work either.

ActionResult:

[AcceptVerbs(HttpVerbs.Get)]
public Epic GetEpicPropertyDetails(int id)
{  
   var Epictemplist = epicRepository.Select().Where(x => x.Id.Equals(id));
   return Epictemplist.SingleOrDefault();
}

javascript:

<script type="text/javascript">
   $(document).ready(function () {
     $(".ListBoxClass").click(function (event) {
       var selectedid = $(this).find("option:selected").val();
       event.preventDefault();
       $.get("/Estimate/GetEpicPropertyDetails", { id: selectedid }, function (result) {
         $(".TimeClass").val(result);
       });
     });
   });
</script>

result.Name is obviously wrong I just dont know how to call this the right way.

share|improve this question
    
try using post and json –  COLD TOLD Apr 14 '12 at 5:14
    
Tried it. :) Json only worked when I was returning a string. It would not work if I tried returning a list. Why idk. I mean I guess I could make get methods in my controller but I shouldn't have to do that –  TMan Apr 14 '12 at 5:17
    
you still cant return it directly like you are doing –  COLD TOLD Apr 14 '12 at 5:27
    
was that a question or r u telling me I can't ? :) –  TMan Apr 14 '12 at 5:33
1  
return Epictemplist.SingleOrDefault(); you cant just return it like this you still need to return it as json –  COLD TOLD Apr 14 '12 at 5:45

2 Answers 2

up vote 1 down vote accepted

Tman, I had a similiar issue that Darin helped me with. I needed to add a $.param to my getJSON. Check out this post MVC ListBox not passing data to Action

share|improve this answer
    
Yea still didn't work. And realize that you were returning just one value 'Values.Length' in your GetCS(). I can return just one value to. But when I try to return an object or a list it does not want to work IDK why. Thx for reply :) –  TMan Apr 14 '12 at 12:24

try changing your method like this

[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetEpicPropertyDetails(int id)
{  
  var Epictemplist = epicRepository.Select().Where(x => x.Id.Equals(id)).SingleOrDefault();
  return Json(Epictemplist, JsonRequestBehavior.AllowGet);
}

Than from your JS

<script type="text/javascript">
$(document).ready(function () {
  $(".ListBoxClass").click(function (event) {
    var selectedid = $(this).find("option:selected").val();
    event.preventDefault();
    $.get("/Estimate/GetEpicPropertyDetails", { id: selectedid }, function (result) {
      $(".TimeClass").val(result.Name);
    }, 'json');
  });
});
</script>
share|improve this answer
    
Nope it calls the jsonresult and passes the id in no problem, but anything inside the $.get doesn't seem to be reached at all. I even tried .getJSON as well –  TMan Apr 14 '12 at 5:30
    
Have you tried specifying the datatype in the get? The default is html if I don't recall wrong (updated answer) –  Iridio Apr 14 '12 at 5:37
    
u mean saying result.Name? Yes..in the jsonresult, if I change Epictemplist to just "HelloWOrld", it'll work..and just result not result.name –  TMan Apr 14 '12 at 5:39
    
Well I'm assuming that Epictemplist is a class with at least a property named Name. You have to adapt my code to your real object obviously. You can use firebug to see what is returned –  Iridio Apr 14 '12 at 6:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.