0

i am sending ajax call to controller where list of objects is filled from database and returned to view. list is stored in TempData and json as well in controller.i have received it in my view but don't know how to display it through TempData or Json result ? how to fix it ?

controller code :

[HttpGet]
public ActionResult Read_Quran()
{
   List<thing> Records = new List< thing> ();

    //database stuff goes here 
   //...
    int count = 0;
    SqlDataReader reader = cmd.ExecuteReader();
    while(reader.Read())
    {
        thing obj = new thing();
        obj.surah  = Convert.ToInt32( reader["Surah_ID"] );
        obj.verse = (string) reader["Ayat_Description"];
        Records.Add(obj); 
        count++;
        if (count == 6236)
            break;
    }
    TempData["message"] = Records;
    connection.Close();
    return Json(new {key= Records }, JsonRequestBehavior.AllowGet);
}

view code :

$.ajax({
   url: "Read_Quran?ayah="+ayah,
   type: 'GET',
   contentType: 'application/json; charset=utf-8',
   dataType: 'json',
   async: false,
   data: '',
   success: function (result) {
       alert("success.!");
       var mera_obj = result.key;
       alert("data is : ???");
   },
   error: function (xhr, ajaxOptions, thrownError) {
       alert("Error :   " + xhr.responseText);
   },
});  

alert in success is hit ...

tereško
  • 58,060
  • 25
  • 98
  • 150

1 Answers1

0

If you store it TempData you don't need to use Ajax, (ofc. if you don't want to dynamically show the value)

Top of your View get your records like this:

var records = TempData["message"];

Then you can display it in your page like this:

foreach (var item in records)
{
    <p> @item.surah </p>
    <p> @item.verse </p>          
}

For AJAX try this:

for (index = 0; index < mera_obj.length; ++index) 
{
     alert(mera_obj[index].surah + "  " + mera_obj[index].verse);
}

And for Arabic font try to add these tags into your HTML:

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1256"> or
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-6">
<META HTTP-EQUIV="Content-language" CONTENT="ar">
Selman Genç
  • 100,147
  • 13
  • 119
  • 184