从JavaScript发送HTML作为参数到ASP.NET MVC控制器

3

我需要使用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 :'( ");
                }

            });
2个回答

1
我建议您针对每个情况使用特定的报告,可以动态生成,但每种类型只需要一个,因为它们都有不同的大小。

我肯定需要以这种方式来做,因为将其适应为所有人的动态报告是不可能的。谢谢兄弟,欢迎来到StackOverflow。 - sGermosen

1
问题在于你将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
}

1
当我尝试时,抛出了以下错误:JavaScript严重错误:http://localhost:13076/Medicals/Patients/DetailsMedicalCertificates/1的第402行,第43列\n\nSCRIPT5017:正则表达式语法错误。 - sGermosen
1
我已经修正了我的答案。 - Victor Leontyev
谢谢,它解决了问题,但是现在我又有一个问题,视图没有渲染HTML,你有什么想法吗? 视图显示如下: *<br><br> Yo: <b>Pepe</b>, exequatur: <b>3333</b> <br> certifico haber examinado a: <b>Lala</b> * 这是我在视图中的HTML代码: `<td> <div> @ViewBag.MyBody</div> </td>` - sGermosen
1
你需要使用 @Html.Raw(ViewBag.MyBody) - Victor Leontyev
1
优秀的人,如果世界上没有像你这样的人,我不知道会发生什么,非常感谢:D - sGermosen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接