3

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?

3 Answers 3

1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

but how can I pass foreach statement to partial view?
0

I'd go ahead and write a custom html helper to do that...It would look something like

@helper YourHelper(Model model){
**DO YOUR FOREACH STUFF IN HERE**
<div class="leftBlock">
    <div class="leftBlockHead">
        <div class="textHead">
            <b>model.Title</b>
        </div>
    </div>
    <div id="leftBlockContent">
        @Html.Raw(model.Content)
    </div>
</div>
}

and then you'd use it..@YourHelper(model)

http://weblogs.asp.net/jgalloway/archive/2011/03/23/comparing-mvc-3-helpers-using-extension-methods-and-declarative-razor-helper.aspx

Comments

0

I have finally used Templated Razor Delegates.

Here is my partial view:

@model LeftColumnBlockViewModel

<div class="levo_blok1">
    <div class="levo_blok1_head">
        <div class="text_head">
            @Model.Title
        </div>
    </div>
    <div id="levo_blok1_content">
        @Model.Content(null)
    </div>
</div>

Here is the view model for it:

public class LeftColumnBlockViewModel
    {
        public string Title { get; set; }
        public Func<dynamic, object> Content { get; set; }

        public LeftColumnBlockViewModel(string title, Func<dynamic, object> content)
        {
            Title = title;
            Content = content;
        }
    }

and here is the usage:

@Html.Partial(MVC.Shared.Views._LeftColumnBlock, 
              new LeftColumnBlockViewModel(
                  Battles.CurrentBattles, 
                  @<text>
                    @foreach (var currentBattle in Model.CurrentBattlesViewModels) 
                    { 
                        <div class="list">
                            @currentBattle.Budget / @currentBattle.BetLimit% / @[email protected]
                        </div> 
                    }
                  </text>))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.