Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jakob Lithner 61 posts 264 karma points
    Mar 17, 2016 @ 19:26
    Jakob Lithner
    0

    Partial view form causes all layout to disappear

    I listened to all Umbraco videos and I am now trying to apply my knowledge. This video talks of partial views as a means to present custom data. http://umbraco.tv/videos/developer/fundamentals/surface-controllers/handling-posts/

    Most of it works, but I am a bit confused. I retrieve data from MediaService to list audio files in different subfolders. The data is presented OK. Then I want to allow the user to swap to another folder by changing a DropDownlist. Then everything goes wrong.

    1) First I am a bit confused on how to trigger on Dropdown change. Should I use jQuery or can Razor helpers help me?

    2) Right now I use a button to trigger new selection. It kind of works. The new data is rendered all right, but all of my layout disappears! The master page with all menus and styling is gone.

    I guess it all boils down to how partial views work together with templates and document types when posting data in the form. It seems that I lost track somewhere ... :)

    Any help very much appreciated!

    Controller

    public class AudioListSurfaceController : SurfaceController
    {
        public ActionResult Index()
        {
            var audioListModel = new AudioListModel();
            var sermonFolder = GetSermonFolder();
            if (sermonFolder != null)
            {
                audioListModel.YearFolderNames = sermonFolder.Children().Select(f => f.Name).ToList();
                audioListModel.YearFolderName = audioListModel.YearFolderNames.OrderByDescending(x => x).FirstOrDefault();
            }
            return Search(audioListModel);
        }
    
        [HttpPost]
        public ActionResult Search(AudioListModel audioListModel)
        {
            var sermonFolder = GetSermonFolder();
            if (sermonFolder != null)
            {
                audioListModel.YearFolderNames = sermonFolder.Children().Select(f => f.Name).ToList();
                var yearFolder = sermonFolder.Children().SingleOrDefault(f => f.Name == audioListModel.YearFolderName);
                if (yearFolder != null)
                {
                    foreach (var mediaItem in yearFolder.Children())
                    {
                        audioListModel.AudioModels.Add(new AudioModel
                        {
                            Title = mediaItem.Name,
                            Author = mediaItem.GetValue<string>("author"),
                            Date = mediaItem.GetValue<DateTime>("date"),
                            Location = mediaItem.GetValue<string>("location"),
                            Size = mediaItem.GetValue<string>("size"),
                            SizeBytes = mediaItem.GetValue<int>("sizeBytes"),
                            Duration = TimeSpan.FromSeconds(mediaItem.GetValue<int>("durationSeconds")),
                            Path = mediaItem.GetValue<string>("umbracoFile"),
                        });
                    }
                }
            }
    
            return PartialView("AudioListForm", audioListModel);
        }
    
    
        private IMedia GetSermonFolder()
        {
            var sermonFolder = Services.MediaService.GetRootMedia().FirstOrDefault(folder => folder.ContentType.Alias == "audioFolder" && folder.Name == "Predikningar");
            return sermonFolder;
        }
    }
    

    Partial View

    @using Sandeslatt.Controllers
    @model Sandeslatt.Models.AudioListModel
    
    @using (Html.BeginUmbracoForm<AudioListSurfaceController>("Search"))
    {
        @Html.DropDownListFor(m => m.YearFolderName, Model.GetYearFolderNames(Model.YearFolderName), new {@class = "form-control"})
    
        <button type="submit" class="btn btn-primary">Sök</button>
    }
    
    <table class="table table-bordered table-condensed table-striped table-nonfluid">
        <tr>
            <th>Datum</th>
            <th>Titel/Talare</th>
            <th class="text-center">Längd</th>
            <th class="text-center">Storlek</th>
            <th class="text-center">Spela</th>
            <th class="text-center">Ladda ner</th>
        </tr>
        @foreach (var audioModel in Model.AudioModels.OrderByDescending(a => a.Date))
        {
            <tr>
                <td>@audioModel.Date.ToString("yyyy-MM-dd")</td>
                <td>
                    <strong>@audioModel.Title</strong><br/>
                    @audioModel.Author
                </td>
                <td class="text-right">@audioModel.DurationDisplay</td>
                <td class="text-right">@audioModel.Size</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        }
    </table>
    

    Hosting Template

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.AudioList>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @{
        Layout = "Master.cshtml";
    }
    
    @Html.Action("Index", "AudioListSurface")
    
  • Phill 115 posts 288 karma points
    Mar 17, 2016 @ 20:54
    Phill
    0

    Hi Jakob,

    Replacing the line:

    
    return PartialView("AudioListForm", audioListModel);
    

    with the following:

    
    return CurrentUmbracoPage();
    

    Not sure if the exact technical reasons, but when you have a Surface Controller with a Post Action, you can't return the partial view to the page (unless it's a ChildAction and not a Post). So returning CurrentUmbracoPage() should resolve the issue.

    Edit: I should note that this will fix the issue of your page not having any formatting. I think you will then have to reconsider how you get your data back to the page.

    Regards, Phill

  • Jakob Lithner 61 posts 264 karma points
    Mar 19, 2016 @ 17:54
    Jakob Lithner
    0

    So you mean I can have my layout back as long as I skip all data I retrieved ... ? That sounds like a strange answer.

  • Jakob Lithner 61 posts 264 karma points
    Mar 19, 2016 @ 17:58
    Jakob Lithner
    0

    Let me rephrase my question:

    I want to create a webpage in Umbraco where I can make same criteria selections in controls (like dropdownlist). When submitted these criteria should produce a list with custom data returned back to the same page. When criteria is modified again the result is rerendered.

    Is this not possible????

  • pat 124 posts 346 karma points
    May 23, 2017 @ 15:58
    pat
    0

    Hi Jakob, know this old post... have you sorted your problem I am facing similar problems .

    if i call renturn PartialView () then it render only view with out master page layout. If I call return to currentumbracopagethen i get error public method not found in controller as macro action name is different.

Please Sign in or register to post replies

Write your reply to:

Draft