c# - Override default html helper with an extension in MVC? -



c# - Override default html helper with an extension in MVC? -

at point it's more of theoretical question, here goes anyway.

the short version: there way create own html helper replace standard html helper (like editboxfor) without going replace route (overriding default helper?) or limit replace function in vs views only?

long version: built mvc app has many many views, our client wants add together functionality allow him alter various text in application. it's related things labels inputs, placeholders , such. wrote own html helper replacement editboxfor (in it's heart still uses , editboxfor) loads placeholder etc. database puts in cache , on. named (oh cleverly) editboxfor2 , takes same inputs editboxfor, in view can add together 2 helper name , supports new text functionality. editboxfor used in scheme many many times , it's not html helper we're modifying. replace of editboxfor editboxfor (or improve yet html.editboxfor( html.editboxfor2() instead of changing few one thousand lines of code hand, it's break views\controllers\other helpers way (i might have forgot implement few variants of editboxfor in html.editboxfor2). wondering if there sort of way override (as in same name , same input parameters) default helpers own?

you can defining display template or editor template (these keywords can google).

long story short, create folder "displaytemplates" , folder "editortemplates" in folder ~/views/shared. it's convention.

then in these folders, can add together template each default editor.

i utilize provide template phone numbers example.

phonenumber.cshtml (should name of class)

@model myproject.common.models.phonenumber <div class="editor-field"> @html.displayfor(model => model.phonenumbertype) @html.displayfor(model => model.phone) </div>

i utilize have display enums

displaytemplates/string.cshtml

@using myproject.website.helpers @{ var type = nullable.getunderlyingtype(viewdata.modelmetadata.modeltype) ?? viewdata.modelmetadata.modeltype; @(typeof(enum).isassignablefrom(type) ? enumviewshelpers.getresourcevalueforenumvalue(model) : model) }

this way, each time have field of type enum display, phone call helper display it. helper search appropriate string in resources.resx.

regarding editor, display dropdownlist select enum want

editortemplates/string.cshtml

@using system.web.mvc.html @using myproject.website.helpers @{ var type = nullable.getunderlyingtype(viewdata.modelmetadata.modeltype) ?? viewdata.modelmetadata.modeltype; @(typeof(enum).isassignablefrom(type) ? html.extenumdropdownlistfor(x => x) : html.textboxfor(x => x)) }

this helper

public static class enumviewshelpers { public static ihtmlstring extenumdropdownlistfor<tmodel, tenum>(this htmlhelper<tmodel> html, expression<func<tmodel, tenum>> expression) { var metadata = modelmetadata.fromlambdaexpression(expression, html.viewdata); var enumtype = nullable.getunderlyingtype(metadata.modeltype) ?? metadata.modeltype; var enumvalues = enum.getvalues(enumtype).cast<object>(); var items = enumvalue in enumvalues select new selectlistitem { text = getresourcevalueforenumvalue(enumvalue), value = ((int)enumvalue).tostring(), selected = enumvalue.equals(metadata.model) }; homecoming html.dropdownlistfor(expression, items, string.empty, null); } public static string getresourcevalueforenumvalue<tenum>(tenum enumvalue) { var key = string.format("{0}_{1}", enumvalue.gettype().name, enumvalue); homecoming resource.resourcemanager.getstring(key) ?? enumvalue.tostring(); } }

i think can utilize same technique editors

c# asp.net-mvc visual-studio

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -