Question

I have created this controller for for getting existing value by searching id. this is my controller for searching data by id. this code is running well but result is not acceptable. i am new in jquery that's why i am explaining this very helpfully..

   public string Search(string id=null)
            {
                string[] ci = new string[9];
                //return "Artistry";

                string cn = null;
                cn = Request.QueryString["id"];
                if (cn != null)
                {
                    ClientInfo c = db.SingleOrDefault<ClientInfo>("where CId='" + cn + "'");
                    if (c != null)
                    {
                       // ci[0] = c.CId.ToString();
                        ci[1] = c.CName;
                        ci[2] = c.CCName;
                        ci[3] = c.PhoneNo.ToString();
                        ci[4] = c.Fax;
                        ci[5] = c.Email;
                        ci[6] = c.Address;
                        ci[7] = c.PostalCode.ToString();
                        ci[8] = c.Country;
                        return ci[5];
                    }
                    else
                        return null;
                }
                else
                    return null;
                 //*/
            }

My view page script for showing my data..

<script type="text/javascript">
    $(document).ready(function () {
        $('#CId').blur(function () {
            var v = $('#CId').val();
            var url = "/Clients/Search/" + v;
          //  alert("Test : " + url);
            $.get(url, function (data, status) {
                $("#CName").val(1);
                $("#CCName").val(2);
                $("#PhoneNo").val(3);
                $("#Fax").val(4);
                $("#Email").val(5);
                $("#Address").val(6);
                $("#PostalCode").val(7);
                $("#Country").val(8);
                alert("Test : " + data + " Status :" + status);
            });
        });
    });

</script>

And finally my sql server database for showing data in views are..

SELECT TOP 1000 [CId]
      ,[CName]
      ,[CCName]
      ,[PhoneNo]
      ,[Fax]
      ,[Email]
      ,[Address]
      ,[PostalCode]
      ,[Country]
  FROM [test].[dbo].[ClientInfo]
Was it helpful?

Solution

I think you should return json type data like so:

public JsonResult Search(string id=null)
    {
         // view code

             return Json(new {info=ci[5]});
    } 

And client code:

$.get(url, function (data, status) {

                alert("Test : " + data.info + " Status :" + status);
            });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top