简体   繁体   English

将对象从Controller传递给JavaScript JQuery

[英]Passing Object From Controller to JavaScript JQuery

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. 我要做的就是将一个Id传递给一个正在运行的ActionMethod并将一个Object返回给javascript。 Then in javascript, I want to be able to say something like..Objec.Property, ie/ Student.Name, or Student.GPA. 然后在javascript中,我希望能够说出像.Objec.Property,即/ Student.Name或Student.GPA这样的内容。

Any help is appreciated. 任何帮助表示赞赏。 I tried json but couldn't get that to work either. 我试过json,但也无法让它工作。

ActionResult: 的ActionResult:

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

javascript: 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. result.Name显然是错的我只是不知道如何以正确的方式调用它。

Tman, I had a similiar issue that Darin helped me with. Tman,我有一个Darin帮助我的类似问题。 I needed to add a $.param to my getJSON. 我需要在我的getJSON中添加$ .param。 Check out this post MVC ListBox not passing data to Action 看看这篇文章MVC ListBox没有将数据传递给Action

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 比你的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM