c# - Output of helper within extension method being swallowed in Razor view -


After this question, I am trying to do something with the following lines:

extension method :

  public static void each & lt; T & gt; (This is IEnumerable & lt; T & gt; i.e., verb & lt; t, int & gt; action) {var i = 0; Foreign exchange (meaning in E) Action (E, I ++); }  

Make a recapitulation in my view using the index, and output results of auxiliary method.

  & lt; Div class = "col-md-12" & gt; @ {Model.Tech (5) .Each ((item, n) => {@RenderItem (item, n = 3 3);}); } & Lt; / Div  

with the following helper

  @helper RenderItem (item items, special = false) {& lt; P & gt; Special items for rendeing here & lt; / P & gt; }  

Although the output is swallowed and the output is not there is a trick to do this work?

Using this extension method, you are not sending anything to the view. Returns of @RenderItem are never sent to the value view, are the Razor HELPERS functions that return only one HellerSalt but to see it, HelperSurst Need to send. When you call an assistant using the @my helper , it renders the HelperStrust because it is that which is @ : Some render does.

But, when you are doing:

  models Take (5). Each ((item, n) = & gt; {@RenderItem (item, n = 3 3);});  

You are just calling your assistant but not (in the end note;) that render anything on the screen. In this case there is no @Render operator, this is only the "Switch to Razor" switch. Possibly you want to render, this is the output of . Each but you can not do this because each has an invalid method.

I played a bit with your code, just to show you these concepts first I used your each method to IEnumerable :

  Public stable IEnumerable & lt; HelperResult & gt; Each & lt; T & gt; (This is IEnumerable & lt; T & gt; i.e., Funk & lt; t, HelperSurst & gt; Action) {var i = 0; Foreign exchange (in E mean) yield return action (e); }  

When looking for a method in HelperResult code (as Func & lt; T, HelperResult & gt; ) If you have a razor expression on it, what can I do in my opinion:

  @newemareble Category (1, 10). Each (i = & gt; @RenderItem (i, i == 3))  

This will apply my RenderItem assistant but the output is only the following (focus Give that, if you put breakpoints in the radiator, the breakpoint will not be killed due to lazy voice, just to add an oval after each call to emphasize the evaluation):

  WebApplication1.FOo + & Lt; Each & gt; D__0`1 [System.INT32]  

(if you used it.) List () this system.collections.Generic.List 1 [System. Web.WebPages.HelperResult] `) will change to something like

This is because the razer knows to present a helperStrust , but IEnumerable & lt; HelperResult & gt; is not what we really are.

So .. what do we have to do? Yes, just reverse the result based on the result (using a standard foreign currency) and to display each result:

  @ {var x = Enumerable.Range (1, 10). Each (i = & gt; @RenderItem (I, I == 3)); Foreign currency (X in x) {@ix}}  

This will work as expected, now you are giving your HelperSult one by one.

Of course, this code is just to show how the RAJAR template works :)

Hope it helps.


Comments