Controller Index Method with String Parameter

Posted by Community Admin on 04-Aug-2018 17:41

Controller Index Method with String Parameter

All Replies

Posted by Community Admin on 10-Oct-2016 00:00

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?

Posted by Community Admin on 10-Oct-2016 00:00

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);

 

Posted by Community Admin on 10-Oct-2016 00:00

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?

Posted by Community Admin on 10-Oct-2016 00:00

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);

Posted by Community Admin on 10-Oct-2016 00:00

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.

This thread is closed