asp.net mvc - MVC 3, Best way to call this HTML -


creating site in mvc 3 , have code snippet use on several parts on design. it's designcode creates head modules on site. wondering if best way call code snippet? should use helpers or there better way?

today this:

    public static ihtmlstring framemoduleheader(this htmlhelper helper, int type, string headline)     {         stringbuilder html = new stringbuilder();             html.append("<div class='module_top'>");                 html.append("<div class='module_top_left'></div>");                 html.append("<div class='module_top_middle_" + type + "'><div class='module_top_headline'><h4>" + headline + "</h4></div></div>");                 html.append("<div class='module_top_right'></div>");             html.append("</div>");         return mvchtmlstring.create(html.tostring());     } 

and call html helpers in view through:

@html.framemoduleheader(1,"my headline")

thanks! /mike

i use partial view or display template include instead of html helper because stuffing html in c# code looks ugly.

so example have view model:

public class someviewmodel {     public int type { get; set; }     public string headline { get; set; } } 

and partial:

@model appname.models.someviewmodel  <div class="module_top">     <div class="module_top_left"></div>     <div class="module_top_middle_@(model.type)">         <div class="module_top_headline">             <h4>@model.headline</h4>         </div>     </div>     <div class="module_top_right"></div> </div> 

and then:

@html.partial("foo", new someviewmodel { type = 1, headline = "my headline" }) 

this of course doesn't mean html helper wouldn't work. it's html helpers should used generate small fragments of html , in particular example doesn't seem case. html intellisense if used in view might aid identify unclosed tags, not formatted html, ... whereas inside c# 1 big magic string.

and 1 final remark html helper if decide use it: make sure html encode headline string before putting inside string builder or might bad xss surprises.


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -