0

I am getting an array object from TagTargets action method to View using ajax call.This method is returning name,value,min,max.I want to append each value in a new table row. I am using for loop for this purpose but nothing is appending in the table.What thing I am doing wrong. Thanks

   function TargetsData() {
    $("#Tag_Targets_div").show();
    var realTags = $('#Raw_Tag_List').val();
    var calculatedTags = $('#Calculated_Tag_List').val();
    $.ajax({
        url:"@Url.Action("TagTargets","CalculatedTags")",
        type: 'Post',
        contentType: 'application/json',
        dataType: 'json',
        data: JSON.stringify({ 'RealTags': realTags, 'CalculatedTags': calculatedTags }),

        success: function (data) {
            debugger;
            if (data != null) {
                for (var i; i <= data.length; i++)
                {
                     $('#tagTargets tbody').append('<tr class="child"><td>'+data[i] +'</td></tr>');
                 }
                }
                else {
                    alert('Not ok');
                }
            }

    });
    }

 [HttpPost]
        public JsonResult TagTargets(List<string> RealTags, List<string> CalculatedTags) 
        {
            List<TagTargetsVM> TagTargetsVMs = new List<TagTargetsVM>();
            foreach (var item in RealTags)
            {
                int IdRealTag = Convert.ToInt32(item);
                TagTargetsVM realObj = db.Raw_Tag_Targets.Where(x => x.Real_Tag_Id_FK == IdRealTag).Select(x => new TagTargetsVM
                {

                    TagName=db.Real_Raw_Points.Where(y=>y.Real_Tag_Id== IdRealTag).Select(z=>z.R_Tag_Name).FirstOrDefault(),
                    Target_Value=x.Target_Value,
                    Minimum_Target=x.Target_Min_Value,
                    Maximum_Target=x.Target_Max_Value
                }).FirstOrDefault();

                TagTargetsVMs.Add(realObj);
            }
            foreach (var item in CalculatedTags)
            {
                int IdCalculatedTag = Convert.ToInt32(item);

                TagTargetsVM CalculatedObj = db.Calculated_Tag_Targets.Where(x => x.C_Tag_Id_FK == IdCalculatedTag).Select(x => new TagTargetsVM
                {
                    TagName = db.Calculated_Tags.Where(y => y.C_Tag_Id == IdCalculatedTag).Select(z => z.C_Tag_Name).FirstOrDefault(),
                    Target_Value=x.Target_Value,
                    Maximum_Target=x.Maximum_Target,
                    Minimum_Target=x.Minimum_Target
                }).FirstOrDefault();

                TagTargetsVMs.Add(CalculatedObj);
            }
            return Json(TagTargetsVMs.ToArray(), JsonRequestBehavior.AllowGet);
        }

//Html Code
<table id="tagTargets">
            <thead>
            </thead>
            <tbody>
            </tbody>
 </table>

enter image description here

11
  • You tagged C#. Do you have C# code related to this issue you need to show? Otherwise, you should remove that tag.
    – lurker
    Commented Apr 24, 2020 at 11:17
  • 1
    Which part are you having trouble with? Appending rows (regardless of ajax) or the ajax call not giving you back what you want/expect? They're 2 very different and unrelated issues. Do you get to the success: callback? What's the result of success: function (data) { console.log(data); ? What about console.log($('#tagTargets tbody').length) ?
    – fdomn-m
    Commented Apr 24, 2020 at 11:17
  • Hi freedomn i am getting the success cal and as well data I just want to append it in table as rows.
    – user11974227
    Commented Apr 24, 2020 at 11:29
  • FYI, if you use @ in front of someone's name, they get a notification. Person asking the question always gets notifications without the @.
    – fdomn-m
    Commented Apr 24, 2020 at 12:34
  • $('#Raw_Tag_List').val() / List<string> RealTags - look at exactly what you're sending. $('#Raw_Tag_List').val() will only give you a single string value, not a list, so would match with string RealTags
    – fdomn-m
    Commented Apr 24, 2020 at 12:35

0