TheEdge
TheEdge

Reputation: 9861

MVC Controller different parameter signature for method

In my base Controller class I have the following defined

    [AcceptVerbs(HttpVerbs.Get)]
    public virtual ActionResult Edit(long Id)
    {
    }

This has been fine up to now. However in the new controller class which descends from above I need to change the method signature to:

   [AcceptVerbs(HttpVerbs.Get)]
    public new ActionResult Edit(string Id)
    {
    }

Note that the parameter Id is now a string. However when the action is invoked on the descendant controller I get the following error:

The parameters dictionary contains a null entry for parameter 'Id' of non-nullable type 'System.Int64' for method 'System.Web.Mvc.ActionResult Edit(Int64)' in 'XXXX.Controllers.MlControllerGridView`2[MakersLane.Web.ViewModels.Login.LoginAddEditViewModel,XXXX.Web.ViewModels.Login.LoginAddEditViewModel]'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

So my question is how do I change the parameter Id to be of type string in my descendant controller and not access any base behaviour?

Upvotes: 2

Views: 849

Answers (1)

TheEdge
TheEdge

Reputation: 9861

I solved this by doing the using the [NonAction] attribute. This way the "long id" is "removed" and it correctly picks up the "string id" version. Not pretty, but it works.

    public ActionResult Edit(string Id)
    {
        return RedirectToAction("NotYetImplemented", "SystemManagement");
    }

    /// <summary>
    ///     To work around the base class requiredment that the Id be a long we override the
    ///     base class and mark this as not an action. Use the string version instead.
    /// </summary>
    /// <param name="Id"></param>
    /// <returns></returns>
    [NonAction]
    public override ActionResult Edit(long Id)
    {
        return RedirectToAction("NotYetImplemented", "SystemManagement");
    }

Upvotes: 1

Related Questions