Controller Index Method with String Parameter - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Controller Index Method with String Parameter

  • Controller Index Method with String Parameter
  • I'd like to develop an MVC widget with an Index method that accepts a string that identifies the particular detail record and then displays that record. I'd like the url to simply be the url of the page the widget resides on, and that id. (for example, say we have an 'agents' page and an ID of 'd039f') the url would look like /agents/d039f

    However, the index method won't accept a conditional string, only a conditional integer value. I know I could get what I want if I create another action in my controller, such as 'details', but then my url gets longer and goes to /agents/details/d039f

    Is there a way to get the shorter url, without having to create another action that can accept the string identifier?

  • Hi Matt,

    You can use HandleUnknownAction method in your controller and resolve view properly

    Example:

    protected override void HandleUnknownAction(string actionName)
            if (//actionname is id and you have agent with specific id)
            
            View("AgentInfo").ExecuteResult(this.ControllerContext);
            
        elsebase.HandleUnknownAction(actionName);

     

  • Thank you, but how will I pass in the model to this view, assuming I have code in this action that retrieves the correct agent?
  • You can call action like that 
                this.Index().ExecuteResult(this.ControllerContext);
    this.Index().ExecuteResult(this.ControllerContext);
    this.Index().ExecuteResult(this.ControllerContext);
    this.Index().ExecuteResult(this.ControllerContext);
    this.Index().ExecuteResult(this.ControllerContext);
  • Unfortunately this is the only widget on the page, and at least when using the Sitefinity Project Manager or Visual Studio to view the site, entering the string does call HandleUnknownAction, but it never renders the view that I call. I've tried View("Index").ExecuteResult(), this.Index().ExecuteResult(), this.ActionInvoker(), etc..

     

    If I add another widget to the page, it works, though.