Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template ASP.NET MVC view

Here is the code in my view for users ratings display:

<div class="leftBlock">
    <div class="leftBlockHead">
        <div class="textHead">
            <b>Users rating</b>
        </div>
    </div>
    <div id="leftBlockContent">
        @foreach (var user in Model)
        {
            <div class="list">
                @user.Login @user.Rating
            </div>
        }
    </div>
</div>

The problem is I use the same html structure for some other blocks. So I don't want to repeat this code, I need some kind of template which will take block title and @foreach as arguments. What are the ways to implement it?

like image 518
SiberianGuy Avatar asked Mar 13 '26 19:03

SiberianGuy


1 Answers

You can look at creating HTMLHelper class extension methods to do such rendering.
You can also use the HTMLHelper RenderPartial method. For this you need to define

  • A separate view class.
  • Have a model associated with it. Call the
  • RenderPartial extension where ever this view needs to be render.
like image 138
Chandermani Avatar answered Mar 16 '26 07:03

Chandermani