c# - Display vs Edit Mode in MVC -


i creating mvc 5 web project collects numerical data user. majority of fields nullable decimals , have requirement display them various decimal places while maintaining full precision when writing database.

the view model has properties such as:

public decimal? tonnesoffoo { get; set; } public decimal? tonnesofbar { get; set; } 

the view renders controls as:

@html.textboxfor(model => model.tonnesoffoo, "{0:n1}", new { @class = "form-control" })  @html.textboxfor(model => model.tonnesofbar, "{0:n3}", new { @class = "form-control" }) 

this works , presents data needed when form posted writing rounded values database.

is there way around without having "actual" , "display" properties in view model , using jquery sync them up? there kind of editor template use handle situation?

update

using suggestion m.ob have managed working.

i no longer using in-line formatting, instead using 2 data attributes, 1 full precision , 1 number of decimal places required. example :

@html.textboxfor(model => model.tonnesoffoo, new { @class = "form-control", data_fullprecision = model.tonnesoffoo, data_precision = 1 }) 

then on document ready loop through , manually apply formatting using jquery number plugin:

$('input[data-fullprecision]').each(function () {      var selector = '#' + this.id;      var precision = parseint($(selector).data('precision'));      var fullprecisionvalue = $(selector).data('fullprecision');       if ($.isnumeric(fullprecisionvalue)) {           var roundedvalue = $.number(fullprecisionvalue, precision, '.', '');           $(selector).val(roundedvalue);      } }); 

i not sure if above overkill needed way present data in correct format if validation errors occurred in controller, without un-rounded values displayed along validation messages.

i use similar scripts map values data attributes when user changes value , map full precision numbers text boxes before form submitted.

there may more elegant way, here 1 idea:

you this: store full precision value in data- attribute of textbox. then, when user changes text, js update data- attribute new value. upon form post, can have js function update text boxes' values data- values before submit occurs. way, of new(updated) values , untouched textbox values posted on not rounded.

@html.textboxfor(model => model.tonnesoffoo, "{0:n1}", new { @class = "form-control", data_fullprecision = model.tonnesoffoo }) 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -