Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 ASP.NET MVC3 HtmlHelper扩展方法,如使用局部视图的BeginForm?_Asp.net Mvc 3_Razor_Html Helper - Fatal编程技术网

Asp.net mvc 3 ASP.NET MVC3 HtmlHelper扩展方法,如使用局部视图的BeginForm?

Asp.net mvc 3 ASP.NET MVC3 HtmlHelper扩展方法,如使用局部视图的BeginForm?,asp.net-mvc-3,razor,html-helper,Asp.net Mvc 3,Razor,Html Helper,我根据SO问题创建了一个扩展方法,效果很好 我是否可以将扩展方法中嵌入的HTML移动到部分视图中,并在保留其当前行为的同时在方法中使用该部分视图?特别是,我希望能够“包装”任意HTML块 我这样做并不是出于任何迫切的需要,只是出于保持HTML一致性的愿望,例如作为视图和部分视图。我想,如果HTML也在视图或部分视图中,那么发现它的任何问题都会容易得多 以下是HtmlHelper扩展方法: public static IDisposable HelpTextMessage(this HtmlHel

我根据SO问题创建了一个扩展方法,效果很好

我是否可以将扩展方法中嵌入的HTML移动到部分视图中,并在保留其当前行为的同时在方法中使用该部分视图?特别是,我希望能够“包装”任意HTML块

我这样做并不是出于任何迫切的需要,只是出于保持HTML一致性的愿望,例如作为视图和部分视图。我想,如果HTML也在视图或部分视图中,那么发现它的任何问题都会容易得多

以下是
HtmlHelper
扩展方法:

public static IDisposable HelpTextMessage(this HtmlHelper helper, bool isHidden, string heading)
{
    TextWriter writer = helper.ViewContext.Writer;

    writer.WriteLine(
        String.Format(
            "<div class=\"help-text {0}\">",
            isHidden ? "help-text-hidden" : ""));

    writer.WriteLine(
        String.Format(
            "<div class=\"help-text-heading\">{0}</div>",
            heading));

    writer.Write("<div class=\"help-text-body\">");

    return new HelpTextMessageContainer(writer);
}
从某种角度来看,我可以使用如下扩展方法:

@using(Html.HelpTextMessage(Model.HelpText.IsHelpTextHidden(Model.SomeHelpMessage), "Something"))
{
    @:To do something, first do something-more-specific, then do another-something-more-specific.
}
@ViewHelpers.HelpTextMessage(false, "Something",
    @:To do something, first do something-more-specific, then do another-something-more-specific.
)
或者我也可以这样使用它:

@using(Html.HelpTextMessage(Model.HelpText.IsHelpTextHidden(Model.SomeHelpMessage), "Something"))
{
    <p>To do something, first do something-more-specific, then do another-something-more-specific.</p>
    <p>Also, keep in mind that you might need to do something-else-entirely if blah-blah-blah.</p>
}
@使用(Html.HelpTextMessage(Model.HelpText.IsHelpTextHidden(Model.SomeHelpMessage),“某物”))
{
要做某事,先做一些更具体的事情,然后再做另一件更具体的事情

此外,请记住,如果诸如此类的话,你可能需要做一些完全其他的事情

}
我还没有找到任何方法将“嵌入的HTML”精确地移动到局部视图中,但是一种更友好的方式来封装HTML,以提供HTML和Razor语法高亮显示和Intellisense,即移动到app app_code文件夹下文件中的视图帮助函数中,如中所述

以下是我的助手函数:

@helper HelpTextMessage(bool isHidden, string heading, Func<object, object> content)
{
    <div class="help-text @(isHidden ? "help-text-hidden" : "")">
        <div class="help-text-heading">
            @heading
        </div>
        <div class="help-text-body">
            @content(null)
        </div>
    </div>
}
或者这个:

@ViewHelpers.HelpTextMessage(false, "Something",
    <p>To do something, first do something-more-specific, then do another-something-more-specific.</p>
    <p>Also, keep in mind that you might need to do something-else-entirely if blah-blah-blah.</p>
)
@ViewHelpers.HelpTextMessage(false,“Something”,
要做某事,先做一些更具体的事情,然后再做另一件更具体的事情

此外,请记住,如果诸如此类的话,你可能需要做一些完全其他的事情

)

我喜欢在视图中嵌入HTML,而不是使用(HTML.HelpTextMessage(…){…}语法使用
,所以我很乐意将此作为解决方案。

刚刚节省了我的时间…:)
@ViewHelpers.HelpTextMessage(false, "Something",
    <p>To do something, first do something-more-specific, then do another-something-more-specific.</p>
    <p>Also, keep in mind that you might need to do something-else-entirely if blah-blah-blah.</p>
)