2

The goal

Create Shared UI Templates with C# + MVC 4 + Razor Engine.

The problem

I do not know how to do.

Details

I have the follow template:

<li>
    <div class="introduction">
        <h3>{productName}</h3>
    </div>
    <div class="presentation">
        <img src="{productImage}" alt="" />
    </div>
    <div class="description">
        <p>Starting by</p>
        <h3>{minProductPrice}</h3>
        <p class="product-unity">/a <span class="product-measure" 
         data-measure-shortcut="{productMeasureShortname">{productMeasureName}
        </span></p>
    </div>
    <div class="controls">
        <button class="button green add" title="Add to comparsion list">+</button>
        <div class="tooltip-quantity">
            <p class="float-left">Quantity:</p>
            <form method="post" action="#">
                <input class="quantity float-left" name="product_quantity"
                maxlength="2" type="text" />
                <span class="float-left">/{productMeasureName}(s)</span>
                <button disabled class="button orange filled float-right">
                   Add
                </button>
            </form>
        </div>
        <button class="button light-blue filled compare float-right" title="This product is available in 6 stores">Compare</button>
    </div>
</li>

and when I want to call it, I would to do something like this:

@Html.RenderMyTemplate("Product Name", "Product Image", "Min Product Price"...)

Logically, the parameters will replace the placeholders on the template.

Any idea?

Thanks in advance!

4

1 回答 1

2

您可以使用部分视图:

@Html.Partial("MyView", Model)

MyView基本上,它使用模型(Model)作为常规剃须刀动力模板呈现给定文件( )。

在你的情况下(我使用string[]as Model,但你可以使用任何你想要的,例如IDictionary<string, string>):

@Html.Partial("MyView", new[] { "Product Name", "Product Image", "Min Product Price" })

鉴于:

@inherits System.Web.Mvc.WebViewPage<string[]>
<li>@Model[0]</li>

更新(带字典)

@Html.Partial("MyView", new Dictionary<string, object> { { "name", "Product1"}, { "price", 100 } })

然后

@inherits System.Web.Mvc.WebViewPage<Dictionary<string, object>>
<li>@Model["name"]</li>
于 2013-06-13T16:03:44.347 回答