【问题标题】:Send Html as parameter from javascript to asp.net mvc controller将 Html 作为参数从 javascript 发送到 asp.net mvc 控制器
【发布时间】:2018-03-24 19:05:40
【问题描述】:

我需要使用 jquery 将一些 HTML 标记传递给控制器​​(请参阅我的 detalle 变量),动作是我尝试发送 detalle 时遇到问题,它说找不到,有人可以帮帮我吗?

           $('#BtnPrint').click(function() {

                var detalle = "<br><br>";

                detalle += " Yo: <b>" + '@Model.DoctorText' + "</b>";
                if ('@Model.Exequartur' != "") {
                    detalle += ", exequatur: <b>" + '@Model.Exequartur' + "</b>    <br>";
                }

                detalle += "   certifico haber examinado a: <b>" + '@Model.PatientName' + "</b> <br>";
                @*if (@Model.ide != "")
                {
                    detalle += " cedula: <b>" + txtcedula.Text + "</b>    <br>";
                }*@

                detalle += " quien presenta: <b>" + '@Model.Affections' + "</b>    <br>";
                detalle += " por lo que recomiendo: <b>" + '@Model.Recomendations' + "</b>    <br>";
                detalle += "<br> dado en: <b>" + ' @Model.Place' + "</b>, " + '@Model.MedicalCertificateDate' +
                    "    <br>";
                detalle += "<br><br><br><br>  ";
                $('#myVar').val(detalle); 



            var win = window.open(
                "@Url.Action("DetailsPrint", "Reports", new {area = "Configurations", id = @Model.Patient.Person.AuthorId, body = detalle, description = "Certificado Medico"})" )  ;

                ////  var win = window.open('http://stackoverflow.com/', '_blank');
                if (win) {
                    //Browser has allowed it to be opened
                    win.focus();
                } else {
                    //Browser has blocked it
                    alert("Porfavor, debes permitir que se abran las ventanas emergentes o el reporte no va a salir :'( ");
                }

            });

【问题讨论】:

    标签: javascript jquery html asp.net-mvc


    【解决方案1】:

    我建议您针对每种情况使用特定的报告,您可以动态地进行,但是,每种类型的报告只有一个,因为它们都有不同的大小

    【讨论】:

    • 我确实需要这样做,因为不可能将其改编为所有人的动态报告,谢谢兄弟,欢迎来到 stackoverflow
    【解决方案2】:

    问题是您将 JS 变量传递给 Url.Action 在你的情况下,你应该这样做:

    $('#BtnPrint').click(function() {
    
        var detalle = "<br><br>";
    
        detalle += " Yo: <b>" + '@Model.DoctorText' + "</b>";
        if ('@Model.Exequartur' != "") {
            detalle += ", exequatur: <b>" + '@Model.Exequartur' + "</b>    <br>";
        }
    
        detalle += "   certifico haber examinado a: <b>" + '@Model.PatientName' + "</b> <br>";
        @*if (@Model.ide != "")
        {
            detalle += " cedula: <b>" + txtcedula.Text + "</b>    <br>";
        }*@
    
        detalle += " quien presenta: <b>" + '@Model.Affections' + "</b>    <br>";
        detalle += " por lo que recomiendo: <b>" + '@Model.Recomendations' + "</b>    <br>";
        detalle += "<br> dado en: <b>" + ' @Model.Place' + "</b>, " + '@Model.MedicalCertificateDate' +
            "    <br>";
        detalle += "<br><br><br><br>  ";
        $('#myVar').val(detalle); 
    
    
        var url = '@Url.Action("DetailsPrint", "Reports", new {area = "Configurations", id = @Model.Patient.Person.AuthorId, description = "Certificado Medico"})';
        url = url + "&body="+encodeURIComponent(detalle);
        var win = window.open(url);
    
        ////  var win = window.open('http://stackoverflow.com/', '_blank');
        if (win) {
            //Browser has allowed it to be opened
            win.focus();
        } else {
            //Browser has blocked it
            alert("Porfavor, debes permitir que se abran las ventanas emergentes o el reporte no va a salir :'( ");
        }
    
    });
    

    你的控制器应该是:

    [ValidateInput(false)]
    public ActionResult DetailsPrint(string body, int id, string description)
    {
        //something
    }
    

    【讨论】:

    • 当我尝试它时,抛出这个错误JavaScript critical error at line 402, column 43 in http://localhost:13076/Medicals/Patients/DetailsMedicalCertificates/1\n\nSCRIPT5017: Syntax error in regular expression
    • 我已经确定了答案
    • 谢谢,它解决了问题,但是,现在我有另一个,在视图上没有呈现 html,知道会发生什么吗?视图显示如下 *

      Yo: Pepe, exequatur: 3333
      certifico haber Examinado a: Lala * 这是我在视图上的 html &lt;td&gt; &lt;div&gt; @ViewBag.MyBody &lt;/div&gt; &lt;/td&gt;
    • 你需要使用@Html.Raw(ViewBag.MyBody)
    • 好人,如果世界上不存在像你这样的人,我不知道会发生什么,太感谢了:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2013-09-28
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 2016-03-02
    相关资源
    最近更新 更多