简体   繁体   English

AJAX将值发送到控制器并返回查询结果

[英]AJAX send value to controller and return query results

I want to pass a value to my controller and execute a query. 我想将值传递给控制器​​并执行查询。 I would then like to return the values from the query to my jquery function so I can then assign these values to various text boxes. 然后,我想将查询中的值返回给我的jquery函数,以便随后将这些值分配给各个文本框。 I can't figure out how to get the data back to jquery. 我不知道如何将数据返回到jquery。 I have only done ajax calls thats have returned a partial view. 我只做过ajax调用,多数民众赞成返回了局部视图。 I am working in ASP.NET 我在ASP.NET中工作

You can use ajax call inside a function like below, you can call this function whenever you need.. 您可以在下面的函数中使用ajax调用,可以在需要时调用此函数。

             function(id){
                     $.ajax({
                                url: "Your Controller/Method path",
                                data: JSON.stringify({
                                    id: id
                                }),
                                dataType: "json",
                                type: "POST",
                                async: false,
                                contentType: "application/json; charset=utf-8",
                                success: function (data) {
                               if(data.success){
                            //Here you will get the value from the Controller if successfully executed 
                           // you get values from data and you can assign those values to the textboxes based on your requirement..             
                              } 
                            }
                           })
                         }

Controller Method: 控制器方式:

        public JsonResult functionName(int id)
        {
          JsonResult result = null;

          try
            {
              var queryValue;
              //here you can put your query and assign it to queryValue and return it back to the UI.
              result = Json(new { success = true, data = queryValue }, JsonRequestBehavior.AllowGet);
            }
          catch (Exception ex)
            {
              result = Json(new { success = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
         return result;
      }

   }

hope this will help you.. 希望这个能对您有所帮助..

Your best bet is to use Json. 最好的选择是使用Json。

Create a C# model server side: 创建一个C#模型服务器端:

var result = new ResultModel
                 {
                    Item1 = "This is a result",
                    Item2 = "Another thing to return",
                    Item3 = 5,
                    ItemArray = new List<string>{ "Thing 1", "Thing 2" }
                 };

return Json(result);

/* Server-side you don't *have* to use a model; an anonymous object is also fine, eg:
 * return Json(new { Item1 = "This is a result" }); etc.
 */

And then your Ajax success function will be ready to accept this Json result: 然后,您的Ajax成功函数将准备接受此Json结果:

$.post(url, data, function(json) {
      $("#textBox1").val(json.Item1);
      $("#textBox2").val(json.Item2);
      // etc....
};

This assumes you're using jquery. 假设您使用的是jquery。 Other frameworks have different client-side syntax. 其他框架具有不同的客户端语法。 Doing Ajax using something like jquery is much nicer than coding it yourself. 使用jquery之类的Ajax比自己编写代码要好得多。

jQuery can usually work out whether you're sending back json or html, but if you get strange results you might need to substitute $.post for $.ajax and specify that you're expecting json in return. jQuery通常可以计算出您是发送回json还是html,但是如果您得到奇怪的结果,则可能需要用$ .post代替$ .ajax并指定您期望返回json。

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

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