model view controller - MVC DisplayFormatAttribute - DataFormatString for strings -
i have string stored in db 12 characters (digits) long. want displayed on screen : "###/####/####"
i use displayformatattribute
[displayformat(dataformatstring = "{0:##/####/#####}", applyformatineditmode = true)]
but provided dataformatstring not seems working.
edit tought solving creating customer displayformatattribute, seems not obvious.
any suggestions ?
i had quite similar problem, , solved using uihint attribute in viewmodel class. in addition created formatter in editortemplates folder. (mvc looks folder default think). happens rendring engine replace editor in view formatter. example bankaccount number 11 digits, have modify case. backend db accept 11 digits no separators, therefore remove these before save.
in view
@html.editorfor(m => m.bankaccount)
in folder views/editortemplates
@model string @{ script.require("common"); } @{string temp = string.empty;} @if (!string.isnullorempty(model)) { if (model.length == 11) { temp = string.format("{0: ####-##-##-###}", convert.toint64(model)).replace("-","."); } else { temp = model; } } <input type="text" id="bankaccount" name="bankaccount" class="form-control span6" onkeypress="numberfilter(event, '.');" value="@temp" />
viewmodel
private string _bankaccount; [uihint("_bankaccountformatter")] public string bankaccount { { return _bankaccount; } set { if (!string.isnullorempty(value)) { _bankaccount = value; _bankaccount = _bankaccount.trim(); _bankaccount = _bankaccount.replace(".", ""); } } }
Comments
Post a Comment