私はMVC 4でHtmlヘルパーを構築しています.HTMLヘルパーでtags/htmlを正しく構築する方法を知りたいです。よりよい解決策であるRazorまたはTag Builderを使用してHTMLヘルパーでHtmlをビルドしますか?
// C#:
public static MvcHtmlString Image(this HtmlHelper html, string imagePath,
string title = null, string alt = null)
{
var model = new SomeModel() {
Path = imagePath,
Title = title,
Alt = alt
};
return MvcHtmlString.Create(Razor.Parse("sometemplate.cshtml", model));
}
// cshtml:
<img src="@model.Path" title="@model.Title" alt="@model.Alt" />
:私はこのような何かを行うことができ、別の側面から
public static MvcHtmlString Image(this HtmlHelper html, string imagePath,
string title = null, string alt = null)
{
var img = new TagBuilder("img");
img.MergeAttribute("src", imagePath);
if (title != null) img.MergeAttribute("title", title);
if (alt != null) img.MergeAttribute("alt", alt);
return MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing));
}
:ここたとえば
はTagBuilder
クラスを使用して画像タグを作成し、単純なHTMLヘルパーですか?
私はテーブルを構築する非常に複雑なヘルパーを持っているので、膨大な数のタグを使用してテーブルを構築するには、非常に遅くなるか遅くなるかを知りたいのです。 –
私は、テーブルのHTMLを構築することはあなたの与えられた例よりも複雑なので、質問に入れました。テーブルを作成するには、MvcContribのグリッドヘルパー - http://mvccontrib.codeplex.com/wikipage?title=GridまたはSystem.Web.HelpersのWebGrid - http://msdn.microsoft.com/en-us/を参照してください。 -us/magazine/hh288075.aspx。私は車輪を再発明しないだろう:) –
私はそれも考えたが、それは私が必要なものではない。私はDBやこのようなものからのデータを表示していません...あなたは私の質問の正解を得るのを助けてくれますか? –