c# - Skip generation of readonly fields for Html.ActionLink -
i have base request-object requestbase
defined like:
public abstract class requestbase { public abstract string area { get; } public abstract string actionname { get; } public abstract string linkname { get; } public abstract string controllername { get; } }
and childclass of like:
public class requesttest : requestbase { public guid id { get; set; } public requesttest() { id = guid.empty; } #region implementation of requestbase public override string area { { return "myarea"; } } public override string actionname { { return "overview"; } } public override string controllername { { return "test"; } } public override string linkname { { return "click me awesome"; } } #endregion }
question. want write helper build links way:
@html.actionlinkbyrequest(new requesttest{id = guid.empty})
which have implemented
public static mvchtmlstring actionlinkbyrequest(this htmlhelper helper, requestbase request, object htmlattributes = null) { return helper.actionlink(request.linkname, request.actionname, request.controllername, request, htmlattributes); }
unfortunately renders to:
<a href="/myarea/test/overview?eventid=00000000-0000-0000-0000-000000000000&actionname=overview&linkname=click%20me%20for%20awesome&controllername=test">click me awesome</a>
but want have only
<a href="/myarea/test/overview?eventid=00000000-0000-0000-0000-000000000000">click me awesome</a>
without readonly fields. because readonly, don't need them explicitly in query-string of action
awaits concrete implementation requesttest
, have them anyway:
public actionresult overview(requesttest request) { // things here return view(); }
any ideas how can skip generation of read-only fields actionlink? maybe using reflection somehow?
edit comments
would work?
public static mvchtmlstring actionlinkbyrequest(this htmlhelper helper, requestbase request, object htmlattributes = null) { var rvd = new routevaluedictionary(); foreach(var prop in request.gettype().getproperties().where(p => p.canread && p.canwrite)) { // add property name , value // rvd.add(prop.name, prop.getvalue()); } // add area (check if not existing) if(!rvd.containskey("area")) rvd.add("area", request.areaname); return helper.actionlink(request.linkname, request.actionname, request.controllername, request, htmlattributes); }
public static mvchtmlstring actionlinkbyrequest(this htmlhelper helper, requestbase request, object htmlattributes = null) { var rvd = new routevaluedictionary(request); rvd.remove("actionname"); rvd.remove("controllername"); rvd.remove("linkname"); return helper.actionlink(request.linkname, request.actionname, request.controllername, rvd, htmlhelper.anonymousobjecttohtmlattributes(htmlattributes)); }
Comments
Post a Comment