TabStrip - how to pass model to partial views using LoadContentFrom

2 posts, 0 answers
  1. Laurie
    Laurie avatar
    141 posts
    Member since:
    Feb 2007

    Posted 25 Aug Link to this post

    I'm trying to pass the model from my parent view through to the partial views that are being loaded in the tabstrip. At this point, what seems to be passed is a List<string> containing the type of the model rather than the model itself, which is a List<string> containing the words "Hello" and "Goodbye". Here's some sample code that demonstrates the issue.

    The controller:

    public ActionResult TabStrip()
    {
        ViewBag.Message = "TabStrip Test";
        List<string> details = new List<string>();
        details.Add("Hello");
        details.Add("Goodbye");
        return View(details);
    }
     
    public ActionResult TabA(List<string> details)
    {
        return PartialView(details);
    }
    public ActionResult TabB(List<string> details)
    {
        return PartialView(details);
    }

    The main View:

    @model List<string>
    @{
        ViewBag.Title = "TabStrip";
    }
     
    <h2>TabStrip</h2>
    @Model[0], @Model[1]
     
    @(Html.Kendo().TabStrip()
                .Name("tabstrip")
                .Items(tabstrip =>
                {
                    tabstrip.Add().Text("Tab A")
                        .Selected(true)
                        .LoadContentFrom("TabA", "Home", new { details = Model });
     
                    tabstrip.Add().Text("Tab B")
                        .Selected(true)
                        .LoadContentFrom("TabB", "Home", new { details = Model });
     
                }))

    The Partial Views:

    Tab A:

    @model List<string>
               <p>Welcome to TabA!</p>
    @Model[0]

    Tab B:

    @model List<string>
               <p>Welcome to TabB!</p>
    @Model[1] -- causes error

    The result upon first loading the page is attached. The first item in the list shows up in the partial view as "System.Collections.Generic.List`1[System.String]" rather than "Hello." Upon clicking Tab B, an exception is thrown because there is no second item in the list.

    So clearly the model is not being passed correctly to the partial views. Any ideas on this?

    Thanks!

    Laurie

     

     

    public ActionResult TabStrip()
    {
        ViewBag.Message = "TabStrip Test";
        List<string> details = new List<string>();
        details.Add("Hello");
        details.Add("Goodbye");
        return View(details);
    }
    public ActionResult TabA(List<string> details)
    {
        return PartialView(details);
    }
    public ActionResult TabB(List<string> details)
    {
        return PartialView(details);
    }
  2. Peter Milchev
    Admin
    Peter Milchev avatar
    135 posts

    Posted 29 Aug Link to this post

    Hello Laurie,

    LoadContentFrom executes a GET request and the data passed to the controller is encoded as query string parameters.

    One option to achieve the desired functionality is to create the collection on the server before calling the PartialView() method instead of passing the collection through the .LoadContentFrom() method. 

    Another option is to serialize the Model in the view and deserialize it in the controller as suggested in this forum thread.

    Regards,

    Peter Milchev
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top