ASP.NET MVC 3 Custom HTML Helpers- Best Practices/Uses -
new mvc , have been running through tutorials on asp.net website.
they include example of custom html helper truncate long text displayed in table.
just wondering other solutions people have come using html helpers , if there best practices or things avoid when creating/using them.
as example, considering writing custom helper format dates need display in various places, concerned there may more elegant solution(i.e. dataannotations in models)
any thoughts?
edit:
another potential use thought of...string concatenation. custom helper take in userid input , return users full name... result form of (title) (first) (middle) (last) depending on of fields available. thought, have not tried yet.
well in case of formatting displayformat attribute nice solution:
[displayformat(dataformatstring = "{0:yyyy-mm-dd}")] public datetime date { get; set; }
and simply:
@html.displayfor(x => x.date)
as far truncating string concerned custom html helper solution.
update:
concerning edit, custom html helper might work in situation there's alternative approach much: view models. if in particular view going show concatenation of names define view model:
public class personviewmodel { public string fullname { get; set; } }
now controller going query repository fetch model , map model view model passed view view @html.displayfor(x => x.fullname)
. mapping between models , view models simplified frameworks automapper.
Comments
Post a Comment