Copied to clipboard

Flag this post as spam?

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


  • Danny Blatant 91 posts 358 karma points
    Oct 28, 2014 @ 18:59
    Danny Blatant
    0

    Question on working with Partial View Macro, List's, Filters (in a form) and Form Submissions

    Hi all,

    Hopefully someone can enlighten me. I have a fairly complex Partial View Maco that is responsible for presenting a list of items, with pagination and filters.

    I have the pagination down, but I am now coding the form for the filters. I've set up the form with Html.BeginUmbracoForm and set up a function in the SurfaceController to receive the post back. This is all fine.

    When I get the postback, I effectively validate and set a session variable. The Model I've built that controls the list is aware of the Session and will filter it's contained List<MyListItem> automatically.

    However I am at a loss as to what to return from the function that recieves the Post, can anyone help me out here? I've tried RedirectToCurrentUmbracoPage() and CurrentUmbracoPage() but these don't work (I get WSOD and Error in partial messages respectively).

    I'm sure I've just got my logic in a muddle, so I hope someone can advise me here.

    Some code samples for better understanding...

    First, here's the PMV View Template (really simple) the only abnormal thing here is I pass the Macro Parameters along with the Action call.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @if (!UmbracoContext.IsFrontEndUmbracoRequest){
        @* This is the RTE Paceholder *@
        @Html.Action("GetPlaceholder", "MySurface", new { macParams = Model.MacroParameters });
    }else{
        @Html.Action("GetList", "MySurface", new { macParams = Model.MacroParameters });
    }
    

    Here's an excerpt of my form on the Partail view (GetList above).

    @using ( Html.BeginUmbracoForm("myFilter", "MySurface"))
    {
        <p>
            Find your thing&nbsp;
            <input type="text" id="mySearch" name="mySearch" placeholder="search" />&nbsp;OR&nbsp;
            <select id="myRegion" name="myRegion">
                <option>pick region</option>
                <option value="" selected="selected" >choose region </option>
                @foreach (RegionPair Region in Model.Regions)
                {
                    <option value="@Region.Key">@Region.Value </option>
                }
            </select>
            <input type="submit" value="submit" />
        </p>
    }
    

    Here's my SurfaceController excerpt (The MyFilters Object is a simple Model that matches the form)

    public class MySurfaceController : SurfaceController
    {
        public ActionResult GetList(IDictionary<string, object> macParams)
        {
            return PartialView("_myList", new MyViewModel(macParams));
        }
    
        public ActionResult GetPlaceholder(IDictionary<string, object> macParams)
        {
            return PartialView("~/Views/RtePlaceholders/_MyPlaceholder.cshtml");
        }
    
        [HttpPost]
        public void MyFilter(MyFilters FormValues)
        {
            if (FormValues.myRegion != null)
            {
                MySessionModel sess = new MySessionModel();
                sess.setSessionVar("Region",FormValues.myRegion.ToString());
            }
            //WHAT DO I RETURN HERE TO RELOAD THE PAGE??
        }
    }
    

    I do hope what I'm trying to achieve makes sence to you, if I can add more info or clarity please ask!

    The underlying Data Model (which drives the list) is 100% functional so I've left it out for simplicity, it's just a wrapper class with a List<MyItem> member, a couple of helpers and a constructor that queries the session on creation. Likewise the ViewModel is a wrapper for the Data Model and simply presents other methods and accessors to the DataModel (I use the view model to transform the data for the view proper, while the data model is a PetaPoco type of affair).

    Have I gone do-lally? Or am I one command shy of what I'm after?

  • Danny Blatant 91 posts 358 karma points
    Oct 29, 2014 @ 12:41
    Danny Blatant
    0

    The solution is I had gone slightly do-lally...

    The correct return I wanted was return CurrentUmbracoPage(); (plus the functinos return type should have been ActionResult that was my type-o).

    What had happened is that the partial view displayed the selected filter options, but deeper in the Models I had set the wrong session variable, causing a null to be returned as a string. This bubbled up to the view to throw an error.

    Kind Regards,

    Danny "Blatant"

Please Sign in or register to post replies

Write your reply to:

Draft