1

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.

Iridio
  • 9,213
  • 4
  • 49
  • 71
TMan
  • 4,044
  • 18
  • 63
  • 117
  • 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 05:17
  • you still cant return it directly like you are doing – COLD TOLD Apr 14 '12 at 05:27
  • was that a question or r u telling me I can't ? :) – TMan Apr 14 '12 at 05: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 05:45

2 Answers2

2

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

Community
  • 1
  • 1
Mustang31
  • 282
  • 1
  • 3
  • 15
  • 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
1

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>
Iridio
  • 9,213
  • 4
  • 49
  • 71
  • 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 05: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 05: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 05: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 06:12