Saturday, 15 September 2012

c# - Pass property expression to method for selecting value from object -



c# - Pass property expression to method for selecting value from object -

what i'm trying create html extension method build grouping of radio buttons in mvc project. know write loop useful method reuse in other projects i'd figure out.

what want able pass look model's property radio buttons (so can name radio buttons same thing , take selected option), collection of objects options, , property of alternative objects used value radio buttons.

here have far doesn't work. (i error "cannot convert method grouping 'value' non-delegate type 'string'. did intend invoke method?").

public static htmlstring radiooptionsfor<tmodel, tproperty>(this htmlhelper<tmodel> htmlhelper, expression<func<tmodel, tproperty>> modelexpression, ienumerable<object> radiooptions, expression<func<object, string>> valueexpression) { stringbuilder sb = new stringbuilder(); var propertyname = ((memberexpression)(modelexpression.body)).member.name; radiooptions.select(el => sb.append(htmlhelper.radiobutton(propertyname, valueexpression.compile()(el)))); homecoming new htmlstring(sb.tostring()); } @html.radiooptionsfor(x => x.category, model.categoryoptions, e => e.value)

update: okay striplingwarrior helped me working solution. issue declaring sec look functions's parameter object. should generic type needed alter method declaration that. added helper method build radio button wrapping label element. did linq stringbuilding don't have loop on set twice. lastly added selecting current value of model , removing duplicate ids of radio button elements. here final solution.

public static string radiobuttonwithlabel<tmodel>(this htmlhelper<tmodel> htmlhelper, string name, string value, string selectedvalue) { var sb = new stringbuilder(); sb.append("<label for=\"").append(name).appendline("\">") .appendline(htmlhelper.radiobutton(name, value, selectedvalue.equals(value), new { id = "" }).tostring()) .append(value).appendline("</label>"); homecoming sb.tostring(); } public static htmlstring radiooptionsfor<tmodel, tproperty, telement>(this htmlhelper<tmodel> htmlhelper, expression<func<tmodel, tproperty>> modelexpression, string selectedvalue, ienumerable<telement> radiooptions, expression<func<telement, string>> valueexpression) { var propertyname = ((memberexpression)(modelexpression.body)).member.name; var result = radiooptions.aggregate(new stringbuilder(), (sb, el) => sb.append(htmlhelper.radiobuttonwithlabel(propertyname, valueexpression.compile()(el), selectedvalue))); homecoming new htmlstring(result.tostring()); }

called this

@html.radiooptionsfor(x => x.category, model.category, model.categoryoptions, e => e.value)

expression<func<object, string>> valueexpression expects look yield string: type of e.value?

it seems e.value shouldn't exist because e object. extension method? if need pass:

e => e.value()

so, deciphering error message: cannot convert "value" (which method or delegate) string that's expected expression: did intend invoke method yield string?

c# expression radiobuttonlist

No comments:

Post a Comment