开发者

How to handle html templates in MVC2?

开发者 https://www.devze.com 2023-01-25 11:58 出处:网络
My (MVC2) application displays several addresses in a View. Each address contains just a subset of information, like first- and lastname. The request is that the complete address information should be

My (MVC2) application displays several addresses in a View.

Each address contains just a subset of information, like first- and lastname. The request is that the complete address information should be displayed when the mouse is over a result.

An html template should be used therefor.

This template defines how(!) the complete address should be displayed – but it doesn’t defines what(!) should be displayed.

Means it can be assumed that the complete address is always “firstname“, “lastname”, “street”, “zipcode” and “city” (for example and to keep it simple). This will never be changed.

But for example the background color can be changed in the html template from white to green or the size of the lastname can be changed from <h1> to <h2>

What is the best way to solve it?

I would prefer to write some shared code (keyword: ascx).

This shared code should wrap the template, would be very easy and would look like this:

&l开发者_Go百科t;div id=”mouseOver” style="display: none;" >
X_REPLACE_X
</div>

The template would look like this simplified example:

FirstName: {Name}<br/>
Lastname: <h1>{Lastname}/<h1><br/>
ZipCode: {ZipCode}<br/>

I would then render the ascx code via “Html.RenderPartial” on the View and map each address to a javascript mouseover function.

The javascript function would replace the placeholder (like {FistName}, {LastName} , etc.) in the template, position and display it.

And thats my problem:

The template should NOT be put directly in the wrapper (ascx - code)!

Means at runtime must “X_REPLACE_X” be replaced with a somewhere on the server stored template.

Because this gives me the ability to change the template without changing and publishing the code!

How can this be managed?

Is there a much better way to solve it? Should I use instead ajax calls to get the template in a variable?

Any help would be really great! thxs in advance!


It sounds like the best way to accommodate your display differences is to simply use different css classes. The html template used for the full information does not change and can be used for all addresses, while the differences between background colors and text size can be handled by different css settings.

Relatedly, I wouldn't handle the different text sizes by using <h1></h1> and <h2></h2> but rather a common tag (such as <span></span>) whose text size is handled via css.

0

精彩评论

暂无评论...
验证码 换一张
取 消