G
G
gimntut2015-08-31 16:03:48
ASP.NET
gimntut, 2015-08-31 16:03:48

How to put repeating fragments into a separate function in razor mvc?

In many places of the template, the same cycle is repeated.
I wanted to put all this into a function, something like this:

@functions{
  // Сильно упрощённый пример
  private void OutItems(string GroupName, Iesi.Collections.Generic.ISet<DataRow> Items) {
    foreach(var Item in Items) {
      @: <div>@Item</div>
    }
  }
}

Sorry @: doesn't work. You can skip this approach and use WriteLiteral, but maintaining the resulting monster is not much easier than many repeating fragments.
Is there any way to use the repeating algorithm as many times as needed without putting it in a separate file?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kovalsky, 2015-08-31
@gimntut

@helper SomeNameMethod(SomeClass object)
{
//Some RazorMarkup
}
//вызывать @SomeNameMethod(object)

Try this
UPD: Well, or really make a PartialView for your collections.

T
Tsiren Naimanov, 2015-08-31
@ImmortalCAT

PatrialView
google further

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question