"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!
You could use the text tag to combine everything into one parameter.
Up vote 2 down vote favorite share g+ share fb share tw.
I'm no guru in Razor syntax, but trying to build a generic library using a fluent-style à là Telerik's GUI components. I have the following pieces (approximately): public static MyBox Box(this HtmlHelper helper) { return new MyBox(helper. ViewContext); } and: /// /// geekswithblogs.net/shaunxu/archive/2010/... /// public class MyBox : IHtmlString { private readonly ViewContext _viewContext; private string _content; private string _title; public MyBox(ViewContext viewViewContext) { _viewContext = viewViewContext; } /// /// See: http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx /// /// /// public MyBox Content(Func value) { _content = value.
DynamicInvoke(_viewContext).ToString(); return this; } /// /// See: http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx /// /// /// public MyBox Content(params Func values) { foreach (var value in values) { _content += value. DynamicInvoke(_viewContext).ToString(); } return this; } public MyBox Content(MvcHtmlString content) { _content = content.ToString(); return this; } public MyBox Title(string title) { _title = title; return this; } public string ToHtmlString() { using (var stringWriter = new StringWriter()) { WriteHtml((TextWriter)stringWriter); return stringWriter.ToString(); } } public void Render() { using (var writer = new HtmlTextWriter(_viewContext. Writer)) WriteHtml(writer); } protected virtual void WriteHtml(TextWriter writer) { writer.
WriteLine(""); writer. WriteLine("" + _title + ")); writer. WriteLine(_content); writer.
WriteLine(""); } } I also have a set of Html extension methods that return MvcHtmlString's. One example is (simplified): public static class GuiElementExtensions { private const string FieldContainerHeadingTemplate = @" {0} "; public static MvcHtmlString GuiFieldContainerHeading(this HtmlHelper helper, string text) { return new MvcHtmlString(String. Format(FieldContainerHeadingTemplate, text)); } } Then, in my .
Cshtml file, I do the following: @using (Html.BeginForm()) { @(Html.Gui() .Box() . Title("") . Content( @ This is the cool Gui.
Box ) ) } Which calls public MyBox Content(Func value), and works. Similarly, when I try the following: @using (Html.BeginForm()) { @(Html.Gui() .Box() . Title("") .
Content( Html. GuiFieldContainerHeading("SubHeading 1") ) ) } It happily calls public MyBox Content(MvcHtmlString content) and works as expected. But, however, when I try to do the following, I can't wrap my head around how the Razor compiler engine works.
How do I get it to return the sum of @ This is Gui().Box() (which is a Func) Html. GuiFieldContainerHeading("SubHeading 1") (which is an MvcHtmlString) as either one object (be it a Func, MvcHtmlString, whatever, or a list of objects? I would like to write generic Razor syntax inside the parameter list to the Content function in my MyBox class, like this: @using (Html.BeginForm()) { @(Html.Gui() .Box() .
Title("") . Content( @ This is Gui().Box() Html. GuiFieldContainerHeading("SubHeading 1") Html.
TextBoxFor(model => model. Name) @ ) ) } Am I on the right track whatsoever, or is there a much simpler way of doing what I want? I want to gather all the "common gui elements" in our system in a common DLL, so not every developer in my organization needs to reinvent the wheel on each project.
Any help appreciated. OK, next problem: I have generalized the Box into a Container, and created two subclasses, Box and HighlightArea. However, using the following code, the Razor compiler kicks me with the message Inline markup blocks (@Content) cannot be nested.
Only one level of inline markup is allowed. The code not working, is: @(Html.Gui() .Box() . Title("BoxTitle") .
Content(@ This is the box content @Html. GuiFieldContainerHeading("This is the heading") @(Html.Gui().HighlightArea() . Content( @Html.
ValidationSummary() @Jalla ) ) ) Do we have a workaround for this? Or is my approach not feasible? Asp.
Net-mvc-3 razor link|improve this question edited Sep 21 '11 at 11:13Bill the Lizard?82.1k42195369 asked Sep 21 '11 at 7:07Erik A. Brandstadmoen1,76847.
1 Your example containing This is Gui().Box() is a bit confusing. It looks as if it contains some C# code but it's probably not supposed to be. – Codo Sep 21 '11 at 7:46 Thanks.
I've updated the question to avoid any confution. Any thoughts on the question other than the confusion? – Erik A.
Brandstadmoen Sep 21 '11 at 7:52 Next problem (thanks, Bill The Lizard, for updating the question... :))... Any thoughts on that (see question) – Erik A. Brandstadmoen Sep 21 '11 at 11:36.
You could use the tag to combine everything into one parameter. @using (Html.BeginForm()) { @(Html.Gui() .Box() . Title("") .
Content( @ This is Gui. Box @Html. GuiFieldContainerHeading("SubHeading 1") @Html.
TextBoxFor(model => model. Name) ) ) } I guess your example isn't really working. If I'm not mistaken you could get it working like this: @ This is Gui.
Box + Html. GuiFieldContainerHeading("SubHeading 1") + Html. TextBoxFor(model => model.
Name) + @ But the tag seems easier.
Worked like a charm. Thanks! :) – Erik A.
Brandstadmoen Sep 21 '11 at 10:37.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.