0

I'm using bootstrap and attempting to use the panel component for rendering lookup items. It renders perfectly fine, but this was all hand coded.

<div class="row">
<div class="col-md-4">
    <div class="panel panel-primary">
        <div class="panel-heading">
            Item Number 1
        </div>
        <div class="panel-body">Image Carousel Goes Here</div>
        <div class="panel-footer">
            <table>
                <tr>
                    <td>Field</td>
                    <td>Value</td>
                </tr>
                <!-- etc. -->
            </table>
        </div>
    </div>
</div>
    <div class="col-md-4">
    <div class="panel panel-primary">
        <div class="panel-heading">
            Item Number 1
        </div>
        <div class="panel-body">Image Carousel Goes Here</div>
        <div class="panel-footer">
            <table>
                <tr>
                    <td>Field</td>
                    <td>Value</td>
                </tr>
                <!-- etc. -->
            </table>
        </div>
    </div>
</div>
</div>

A JSFiddle is here.

I'm wondering how it'd be possible to do something like:

<div class="row">
   @(foreach Item item in items)
   {
        @RenderItemView(item);
   }
</div>

where I could dump all of the "div.col-md-4 ... slash div" code into another cshtml file and use it as a component and just pass the model to it?

I know that I could do this, but would prefer to make this a reusable component.

 <div class="row">
    @foreach(Item item in items)
    {
         // instead of using a component, setting the panel-heading off item.Number,
         // panel-body by a foreach on item.ImageUrls, etc.
    }
 </div>

Basically, the subcomponent will be a partial view, I think, but what is the normal way of passing a model to it and it rendering at that spot (basically, like @RenderBody works on layout)

1 Answer 1

1

You could create a partial view which holds this content:

@model IEnumerable<ItemType>

<div class="row">
    @foreach(var item in Model)
    {
         // instead of using a component, setting the panel-heading off item.Number,
         // panel-body by a foreach on item.ImageUrls, etc.
    }
</div>

Save the partial view in the Views/Shared folder, so that it's available everywhere. Then to use it:

@{Html.RenderPartial("YourPartialView", Model.Items);}

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that seems much cleaner than the way I've been doing it.

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.