用jQuery(?)将html加载到MVC页面

时间:2011-02-22 19:42:54

标签: jquery asp.net-mvc

这就是事情。

我在MVC中有一个页面。此页面有帮助。

<% Html.RenderPartial("foldingHelpBox", ViewData["foldingHelpBox"]); %>

所以行动:

    public ActionResult Index(int? verbID, string schooljaarparam) {
        ......

        ViewData["foldingHelpBox"] = new InfoFlyout() { Title = "How does this work?", Message =  "foldinghelpbox.html" }; 

        return View();
    }

什么是InfoFlyout?

public partial class InfoFlyout {
    public string Title { get; set; }
    public string Message { get; set; }
}

我想要什么?

我想要像Accordeon一样的效果,标题和信息。默认情况下,该消息将关闭,并在您单击标题时打开。但是我只想要一件物品,而不是像手风琴那样的多件物品。

jQuery:

$(document).ready(function () {
    $(".foldingHelpText").hide();
    $(".foldingHelp").click(function () {
        var helptext = $(this).siblings(".foldingHelpText");
        if (helptext.is(":hidden")) {
            alert(helptext.attr("data"));
            helptext.load(helptext.attr("data"));
        }
        $(this).siblings(".foldingHelpText").toggle("blind", { direction: "vertical" }, 1000);
        return false;
    });

    $(".closedialog").click(function () {
        $(this).parent().hide("blind", { direction: "vertical" }, 1000);
    });
});

部分

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MVC2_NASTEST.Models.InfoFlyout>" %>
<div class="foldingHelpBox">
    <a class="foldingHelp" href="#">
        <%= Model.Title %></a>
    <div class="foldingHelpText" data="<%= Model.Message %>">
        <span class="closedialog ui-icon ui-icon-closethick" title="Sluiten">&nbsp;</span>
    </div>

告诉我这个问题!

嗯,Message可以是两件事。它可以是包含消息的字符串,也可以是一个字符串,实际上是HTML文件的名称,其中包含要放入消息中的实际html。这个html可能相当长,这就是为什么我不想用1个字符串写所有内容。

问题是我使用MVC并且HTML文件位于我的View文件夹中。我可以把它移到其他地方。

我想要DRY代码。

有两个问题:

你会怎么做? 我现在正在尝试使用jQuery load函数将html读入foldinghelptext div,但我无法获得html文件的URL。那你怎么做的?也许把HTML放在一个部分,命名它。整个地方看起来并不那么干净。

如何获取此html文件的网址

如果我这样做的方式很好,我如何获取html文件的URL,让jQuery加载它。

4 个答案:

答案 0 :(得分:1)

如何获取此html文件的网址

要获取html文件的限定路径,请尝试使用Url.Content()

  

将虚拟(相对)路径转换为   应用程序绝对路径。

<div class="foldingHelpText" data="<%=Url.Content("~/SomePathToHtmlFile/" +  Model.Message )%>">
  <span class="closedialog ui-icon ui-icon-closethick" title="Sluiten">&nbsp;</span>
</div>

答案 1 :(得分:1)

DRIest要做的就是始终从html或字符串加载消息,而不是同时支持两者。

在这种情况下,我会选择html。

答案 2 :(得分:0)

在Controller中创建一个Action,让方法返回

return base.File("~/WhateverPath/htmlPage.htm", "text/html");

然后你可以拥有你想要的任何网址。

答案 3 :(得分:0)

ok为什么我喜欢stackoverflow:当我写完我的问题后,我得到一个好主意,通常可以解决问题:D

我做了什么?

我将HTML文件中的html放在partial

然后,在动作

        ViewData["foldingHelpBox"] = new InfoFlyout() { Title = "Hoe werkt dit?", Message = "partialname" };

主要部分:

<div class="foldingHelpBox">
    <a class="foldingHelp" href="#">
        <%= Model.Title %></a>
    <div class="foldingHelpText">
        <span class="closedialog ui-icon ui-icon-closethick" title="Sluiten">&nbsp;</span>
        <% Html.RenderPartial(Model.Message); %>
    </div>
</div>
滑动打开和关闭的

jQuery

$(document).ready(function () {
    $(".foldingHelpText").hide();
    $(".foldingHelp").click(function () {
        $(this).siblings(".foldingHelpText").toggle("blind", { direction: "vertical" }, 1000);
        return false;
    });

    $(".closedialog").click(function () {
        $(this).parent().hide("blind", { direction: "vertical" }, 1000);
    });
});

所以现在我只有,正如jfar建议的那样,html输入非常干