asp.net mvc - How to merge htmlAttributes in Custom Helper -
asp.net mvc - How to merge htmlAttributes in Custom Helper -
i have custom helper receive htmlattributes parameter:
public static mvchtmlstring campo<tmodel, tvalue>( htmlhelper<tmodel> helper, expression<func<tmodel, tvalue>> expression, dynamic htmlattributes = null) { var attr = mergeanonymous(new { @class = "form-control"}, htmlattributes); var editor = helper.editorfor(expression, new { htmlattributes = attr }); ... } the mergeanonymous method must homecoming merged htmlattributes received in parameter "new { @class = "form-control"}":
static dynamic mergeanonymous(dynamic obj1, dynamic obj2) { var dict1 = new routevaluedictionary(obj1); if (obj2 != null) { var dict2 = new routevaluedictionary(obj2); foreach (var pair in dict2) { dict1[pair.key] = pair.value; } } homecoming dict1; } and in editor template illustration field need add together more attributes:
@model decimal? @{ var htmlattributes = htmlhelper.anonymousobjecttohtmlattributes(viewdata["htmlattributes"]); htmlattributes["class"] += " inputmask-decimal"; } @html.textbox("", string.format("{0:c}", model.tostring()), htmlattributes) what have in htmlattributes @ lastly line in editor template is:
click here see image
note "class" appearing correctly, others attributes extension helper in dictionary, doing wrong?
if possible, want alter extension helper , not editor template, think routevaluedictionary passed editorfor need cast anonymous object...
i solved changing editor templates line:
var htmlattributes = htmlhelper.anonymousobjecttohtmlattributes(viewdata["htmlattributes"]); for this:
var htmlattributes = viewdata["htmlattributes"] idictionary<string, object> ?? htmlhelper.anonymousobjecttohtmlattributes(viewdata["htmlattributes"]); and mergeanonymous method this:
static idictionary<string,object> mergeanonymous(object obj1, object obj2) { var dict1 = new routevaluedictionary(obj1); var dict2 = new routevaluedictionary(obj2); idictionary<string, object> result = new dictionary<string, object>(); foreach (var pair in dict1.concat(dict2)) { result.add(pair); } homecoming result; } asp.net-mvc attributes extension-methods
Comments
Post a Comment