+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Controller Index Method with String Parameter

Controller Index Method with String Parameter

5 posts, 0 answered
  1. Matt
    Matt avatar
    0 posts
    Registered:
    11 Jun 2013
    10 Oct
    Link to this post

    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?

  2. Victor Leontyev
    Victor Leontyev avatar
    65 posts
    Registered:
    01 Jul 2014
    10 Oct
    Link to this post

    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);
            }
        else{base.HandleUnknownAction(actionName);}
    }

     

  3. Matt
    Matt avatar
    0 posts
    Registered:
    11 Jun 2013
    10 Oct in reply to Victor Leontyev
    Link to this post
    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?
  4. Victor Leontyev
    Victor Leontyev avatar
    65 posts
    Registered:
    01 Jul 2014
    10 Oct in reply to Matt
    Link to this post
    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);
  5. Matt
    Matt avatar
    0 posts
    Registered:
    11 Jun 2013
    10 Oct in reply to Victor Leontyev
    Link to this post

    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.

5 posts, 0 answered