【问题标题】:how to show json result into a bootstrap modal in mvc如何在 mvc 中将 json 结果显示为引导模式
【发布时间】:2015-04-24 17:50:57
【问题描述】:

我有以下代码来显示 word 文档,我将结果显示到文本区域,但不知道如何在 mvc 的引导模式中实现它。我正在关注提到的链接,任何人都可以帮助我实现这一目标。

following this link

这是我的控制器代码。

 public JsonResult ReadTextFile(string fName)
        {
            string retString = string.Empty;
            string path = Path.Combine(Server.MapPath("~/Media"), fName);
            if (System.IO.File.Exists(path))
            {
                if (Path.GetExtension(path) == "doc" || Path.GetExtension(path) == ".docx")
                {
                    Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                    object miss = System.Reflection.Missing.Value;
                    object readOnly = true;
                    object wordPath = path;
                    Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(
                        ref wordPath,
                        ref miss,
                        ref readOnly,
                        ref miss, ref miss, ref miss,
                        ref miss, ref miss, ref miss,
                        ref miss, ref miss, ref miss,
                        ref miss, ref miss, ref miss, ref miss);
                    for (int i = 0; i < docs.Paragraphs.Count; i++)
                    {
                        retString += " \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString();
                    }
                }
                else if (Path.GetExtension(path) == "txt")
                {
                    using (StreamReader sr = new StreamReader(path))
                    {
                        retString = sr.ReadToEnd();
                    }
                }
            }
            return Json(retString, JsonRequestBehavior.AllowGet);
        }

【问题讨论】:

    标签: json asp.net-mvc-4


    【解决方案1】:

    使用 ActionLink

    @Html.ActionLink("Preview", "ReadTextFile", "Marketing",
    routeValues: new {  
    fName = @item.Content,
    },                                                               
    htmlAttributes: new
    {                                                                            id="btnClick1",
    name = @item.Content
    })
    

    然后在你的视野中

    <div id='loadDocument' class='modal fade in'>
                                        <div class="modal-dialog">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <div class="row">
                                                        <div class="col-md-10 col-sm-10">
                                                           Preview Document
                                                        </div>
                                                        <div class="col-md-1 col-sm-1 pull-right">
                                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="modal-body">
                                                    <textarea id="txtloadData" rows="600" class="form-control"style="width:100%; height:1000px"></textarea>
                                                </div>
                                                </div>
                                        </div>
                                    </div>
    

    将此添加到页面底部

    <script>
        $("#btnClick1").click(function (e) {
            e.preventDefault();
            $.ajax({
                type: "GET",
                url: '@Url.Action("ReadTextFile", "Marketing")',
                data: { fName: $(this).attr("name") },
                DataType: "text",
                success: function (str) {
                    $("textarea#txtloadData").val(str);
                    $('#loadDocument').modal('show');
                },
                error: function (str) {
                    alert("failed");
                }
            });
        });
    
    </script>
    

    希望对您有所帮助。如果您在实施时遇到任何问题,请告诉我。

    【讨论】:

      【解决方案2】:

      您将需要使用 JQuery 来显示数据,或者另一种方法是将模型作为 ActionResult 传递回 View。然后在您的 View 中使用 HTML 帮助程序显示它,如下所示:

      `@Html.Raw(Json.Encode(Model.Values))`
      

      这里有 2 个链接可以指导您找到正确的解决方案:

      How to return Json object from MVC controller to view

      MVC4 - Displaying JSON result properties in view

      【讨论】:

        猜你喜欢
        • 2016-09-03
        • 1970-01-01
        • 2014-08-08
        • 2016-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-10
        相关资源
        最近更新 更多