mercredi 30 septembre 2015

Skip generation of readonly fields for Html.ActionLink

I have a 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 a childclass of this like:

public class RequestTest : RequestBase
{
    public Guid Id { get; set; }

    public RequestTest()
    {
        Id = Guid.Empty;
    }

    #region implementation of RequestBase

    public override string Area
    {
        get { return "MyArea"; }
    }

    public override string ActionName
    {
        get { return "Overview"; }
    }

    public override string ControllerName
    {
        get { return "Test"; }
    }

    public override string LinkName
    {
        get { return "Click me for awesome"; }
    }

    #endregion
}

Question. I want to write a helper to build links this way:

@Html.ActionLinkByRequest(new RequestTest{Id = Guid.Empty})

which I currently 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 it renders to:

<a href="/MyArea/Test/Overview?EventId=00000000-0000-0000-0000-000000000000&ActionName=Overview&LinkName=Click%20me%20for%20awesome&ControllerName=Test">Click me for awesome</a>

but I want to have only

<a href="/MyArea/Test/Overview?EventId=00000000-0000-0000-0000-000000000000">Click me for awesome</a>

without the readonly fields. Because they are readonly, I don't need them explicitly in the query-string as of my Action awaits the concrete implementation RequestTest and will have them anyway:

public ActionResult Overview(RequestTest request)
{
    // do things here

    return View();
}

Any ideas how I can skip the generation of read-only fields for the actionlink? Maybe by using reflection somehow?





Aucun commentaire:

Enregistrer un commentaire