简体   繁体   English

MVC-在控制器中获取HTML标签

[英]MVC - Get Html tag in Controller

I have a section in my website where I want to send as an email to user when they click on button send. 我的网站上有一个部分,当用户单击“发送”按钮时,我想将其作为电子邮件发送给用户。 How can I pass the HTML and it's content to controller and then passing it as the message body to smtpClient 如何将HTML及其内容传递给控制器​​,然后将其作为消息正文传递给smtpClient

I have 我有

<div id="mydiv">
<div>
<a>Click Here</a>
</div>

</div>

I will like to get all the html and it's content (<div id="mydiv"><div><a>Click Here</a></div></div>) when the id mydiv is called from the controller 我想从控制器调用id mydiv时获取所有html及其内容(<div id="mydiv"><div><a>Click Here</a></div></div>)

You can use jQuery to intercept the click and construct an ajax request that includes the html of your choice. 您可以使用jQuery截获点击,并构造一个包含您选择的html的ajax请求。 Accept the html as a string argument with a matching name. 接受html作为具有匹配名称的字符串参数。 Then use the smtp client to forward it to the address of your choice. 然后使用smtp客户端将其转发到您选择的地址。

<script>
$(document).ready(function () {
    $(document).on("click", "#button1", {}, function (event) {
        event.preventDefault();

        var x = $("#mydiv").html();

        var options = {
            url: "your url",
            method: "POST",
            data: {
                data1: x
            }
        };

        var req = $.ajax(options);

        req.done(function (resp) {
            alert("Email sent");
        });

    });
});
</script>

<div id="mydiv">
<div>
    <a id="button1">Click Here</a>
</div>
</div>


public IActionResult TestAction(string data1)
{
        SmtpClient client = new SmtpClient("smtp_server");
        client.UseDefaultCredentials = false;
        client.Credentials = new NetworkCredential("username", "password");

        MailMessage mailMessage = new MailMessage();
        mailMessage.From = new MailAddress("test@test.com");
        mailMessage.To.Add("test@test.com");
        mailMessage.Body = data1;
        mailMessage.Subject = "subject";
        client.Send(mailMessage);

        return null;//or whatever you want
}

I would use an html editor, check ckeditor for example. 我会使用html编辑器,例如检查ckeditor and here is a tutorial on how to implement it with mvc and when you install the CKEDITOR make sure the config htmlEncodeOutput is true. 是关于如何使用mvc实施的教程,并且在安装CKEDITOR时,请确保confightmlhtmlcodeOutput为true。 If you used it, the action might reject the request because it contains dangerous input, you will have to set the validate input to false, or check this for more details 如果使用了该操作,则该操作可能会由于包含危险输入而拒绝该请求,因此您必须将validate输入设置为false,或检查此信息以获取更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM