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.

4
  • 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 Commented Apr 14, 2012 at 5:17
  • you still cant return it directly like you are doing Commented Apr 14, 2012 at 5:27
  • was that a question or r u telling me I can't ? :) Commented Apr 14, 2012 at 5:33
  • 1
    return Epictemplist.SingleOrDefault(); you cant just return it like this you still need to return it as json Commented Apr 14, 2012 at 5:45

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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 :)
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>

4 Comments

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
Have you tried specifying the datatype in the get? The default is html if I don't recall wrong (updated answer)
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
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.