0

i have a modal with input, i digit some emails and add to a list, later i want to pass this list of emails to my function that send emails.

        var listEmails = [];
    document.getElementById("addEmail").onclick = function () {

        var text = document.getElementById("recipient-email").value;

        $("#Listmail").append('<li>' + text + '</li>');

        listEmails.push(text);

    }

    document.getElementById("sendEmail").onclick = function () {


       @*location.href = '@Url.Action("TestSendReport", "ProductMarketplace")?emails='+listEmails;

    }

that is my function in Controller that receive a list of email to send

         public void TestSendReport(List<string> ListMails)
3
  • Is there a reason not to use jQuery with HttpPost? Commented Mar 12, 2018 at 21:49
  • im new on this javascript Commented Mar 12, 2018 at 22:12
  • It is very basic in ASP.NET MVC. I suggest you to watch this free PluralSight Video by Scott Allen. Commented Mar 12, 2018 at 23:19

2 Answers 2

1

Please try below code and try to call controller method using jQuery Ajax

    var list= [];
document.getElementById("addEmail").onclick = function () {

    var text = document.getElementById("recipient-email").value;

    $("#Listmail").append('<li>' + text + '</li>');

    list.push(text);

}

document.getElementById("sendEmail").onclick = function () {
    var jsonText = JSON.stringify({ list: list});

 $.ajax({
            type: "POST",
            url: "ProductMarketplace/TestSendReport",
            data: jsonText,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function() { alert("success"); },
            failure: function() { alert("failed"); }
        });

}

And use controller method like this,

[WebMethod]
        public void TestSendReport(List<string> list)
        {

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

Comments

0
document.getElementById("sendEmail").onclick = function () {
   location.href = '@Url.Action("TestSendReport", "ProductMarketplace")?emails=' + 
      encodeURI(JSON.stringify(listEmails));
}

public void TestSendReport(List<string> emails)
{
}

1 Comment

thx but still not pass my list my method receive a empety list

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.