1

I have this code on back-end controller

 [HttpGet]
    public ActionResult EmailsList()
    {
        var itemsEmail = db.InvitationMails
            .Select(x=> new
            {
                Email = x.To.ToString(),
                Name = x.Name.ToString(),
            })
            .ToList();
        return Json(itemsEmail, JsonRequestBehavior.AllowGet);
    }

This on AJAX call code

<script>
$('#save_quest').click(function () {
    email_update();
});

function email_update() {
    $.ajax({
        url: '@Url.Action("EmailsList", "Questions")',
        contentType: 'application/json; charset=utf-8',
        type: 'GET',
        dataType: 'json',
        processData: false,
        success: function (result) {
            var email = result;
           // console.log(result[0].Name);
            for (var i = 0; i <= email.length - 1; i++){
                var emailHTML = '<div style="margin-left: 25px; margin-top: 10px;>' + '<b style="margin-left: 10px;">' + result.Email + '<b>' + '<b style="margin-left: 20px;">' + result.Name + '</b>' + '</div>'
                $(".email_list").append(emailHTML);
            }
        }
    });
}

Code works well,but I have undefined on View.

enter image description here

How I need to edit my code to display it correctly?

3
  • Do like this result[i].Email and result[i].Name Commented Apr 12, 2017 at 12:03
  • 1
    Yes. Thank's dude @BasantaMatia Commented Apr 12, 2017 at 12:06
  • Happy to know that it's solved your issue. You can mark as answer my solution to help someone :) Commented Apr 12, 2017 at 12:16

1 Answer 1

2

I'm writing this to help someone if he/she get the same issue,

Because of it's an array of objects, so you need to add [i] with your result inside your for loop.

for (var i = 0; i <= email.length - 1; i++)
  {
   var emailHTML = '<div style="margin-left: 25px; margin-top: 10px;>' + '<b style="margin-left: 10px;">' + result[i].Email + '<b>' + '<b style="margin-left: 20px;">' + result[i].Name + '</b>' + '</div>'
   $(".email_list").append(emailHTML);
  }

Cheers :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.