c# - Data-Driven list of numerical measures in a view returning as strings -
i've got list of measures in database:
measures id displayorder name ------------------------------ 1 3 homework 2 2 quiz 3 1 exam 4 4 somebigint ...etc
some of these measures represented int
s, double
s.
i'm trying come way these measures out of database , make displays them:
model
namespace myproj.businessrepository.buisnessmodels { public interface imeasure { int32 id { get; set; } string name { get; set; } /// <summary> /// position in measure should displayed /// </summary> int32 displayorder { get; set; } } public class scoremeasure<t> : imeasure { public int id { get; set; } public string name { get; set; } public int displayorder { get; set; } public t adjustment { get; set; } public t basevalue { get; set; } } public class adminmeasure<t> : imeasure { public int id { get; set; } public string name { get; set; } public int displayorder { get; set; } public t weight { get; set; } } }
which use inside parent model list (which wrapped in viewmodel).
after reading on various other questions on so, looks way (given multiple types) list<scoremeasure<object>>
.
view
for(var i=0; < model.scorecard.measures.count; i++) { @html.hiddenfor(m=>m.scorecard.measures[i].name) @f.formgroup().textboxfor(m => m.scorecard.measures[i].adjustment).append("%").label().labeltext(model.scorecard.measures[i].name) }
it's working, adjustments coming strings (which suppose makes sense since had use object
generic parameter).
is there smarter way these recognized numbers? is, unobtrusive validation doesn't enforce numeric constraint, , have parse them when come (not sure how i'm going tell int vs double other maybe looking decimal point, removed user)?
eta: don't confused funky @f.formgroup()
stuff in view, that's twitterbootstrapmvc-- it's wrapping @html.textboxfor
.
to answer question how format value, can decorate model displayformat
attribute. used in conjuncture html.displayfor
or @html.displayformodel
method render it.
public class mymodel { [displayformat(dataformatstring = "{0:n0}" ,applyformatineditmode = true)] public decimal intvalue { get; set; } public decimal decimalvalue { get; set; } }
using object initializer of var model = new mymodel {decimalvalue = 2.546m, intvalue = 245.110m};
shows decimal, displayfor
, editorfor
methods format appear int user.
you can use editor , display templates accomplish , decorate property uihint
attribute tell displayfor
, editorfor
methods use templates create render it.
Comments
Post a Comment