Copied to clipboard

Flag this post as spam?

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


  • suzyb 474 posts 932 karma points
    Sep 24, 2013 @ 12:11
    suzyb
    0

    Controller postback current page

    I'm trying to (eventually) create a partial view macro that will allow visitors to select a value from a dropdown, submit a form and view the related data for the entry they selected.

    I can't seem to get the post to work however.  I have a method to handle the form post in my controller but I don't know how to then return to the same page with the model updated with the new data.

    This is my controller code

    [HttpPost]
    public ActionResult HandleDropDownPostback(ConstituencyModel model)
    {
        var db = ApplicationContext.Current.DatabaseContext.Database;
        var ccgs = db.Query<CCG>("SELECT * FROM bcg_CCGs WHERE ConstituencyCrossover = '@0'", model.ConstituencySelected);
    
        model.Ccgs = ccgs;
    
        return PartialView("ConstituencyDropDown", model);
    }

    But when the form is submitted all I get is a plain page with the form on it.

    Can anyone help.

  • suzyb 474 posts 932 karma points
    Sep 25, 2013 @ 13:32
    suzyb
    0

    Managed to hack something together.  Although I'd like to know the correct way to do something like this.

    This is my controller

    [HttpPost]
    public ActionResult HandleDropDownPostback(ConstituencyModel model)
    {
        if (ModelState.IsValid == false)
        {
            return CurrentUmbracoPage();
        }
    
        var db = ApplicationContext.Current.DatabaseContext.Database;
    
        Constituency constituency = db.Query<Constituency>("SELECT * FROM bcg_Constituencies WHERE ConstituencyName = @0", model.ConstituencySelected).First();
        if (constituency != null)
        {
            TempData["constituency"] = constituency;
        }
    
        var ccgs = db.Query<CcgModel>("SELECT * FROM bcg_CCGs LEFT JOIN bcg_CcgContactDetails ON bcg_CCGs.CCGName = bcg_CcgContactDetails.CCGName WHERE ConstituencyCrossover = @0", model.ConstituencySelected);
        TempData["ccgs"] = ccgs;
    
        return RedirectToCurrentUmbracoPage();
    }

    And my partial view

    @using (Html.BeginUmbracoForm<ConstituencySurfaceController>("HandleDropDownPostback"))
    {
        <div class="editor-row">
            @Html.DropDownListFor(model => model.ConstituencySelected, Model.ConstituencyList, "Select your constituency")
    
            <input type="submit" value="Find CCG Details" class="btn auto" />
            <div>
            @Html.ValidationMessageFor(model => model.ConstituencySelected)
            </div>
        </div>
    
        if (TempData["constituency"] != null && TempData["ccgs"] != null)
        {
            Constituency constituency = (Constituency)TempData["constituency"];
    
            <div class="constituency-details">
                <p><strong>Constituency:</strong> @constituency.ConstituencyName<br />
                    <strong>MP:</strong> @constituency.FirstName @constituency.LastName (@constituency.Party)
                </p>
            </div>
    
            IEnumerable<CcgModel> ccgs = (IEnumerable<CcgModel>)TempData["ccgs"];
    
            if (ccgs.Count() > 0)
            {
                <table class="ccg-table">
                    <tr>
                        <th>CCG</th>
                        <th style="width:230px;">Contact Phone No</th>
                        <th style="width:40px;">Live</th>
                    </tr>
                @foreach (CcgModel item in ccgs)
                {
                    <tr class="@(item.Live ? "live" : "notlive")">
                        <td>@item.CCGName</td>
                        <td>@(!string.IsNullOrEmpty(item.Telephone) ? item.Telephone : "N/A")</td>
                        <td>@(item.Live ? "Yes" : "No")</td>
                    </tr>
                }
                </table>
            }
            else
            {
                <p>There are no CCGs within the selected constituency.</p>
            }
        }
    }

     

  • Charles Afford 1163 posts 1709 karma points
    Sep 28, 2013 @ 23:08
    Charles Afford
    0

    Firstly i would not use umbracoBeginForm or RedirectToPage

    I would use standard Html and Ajax posts

    It sounds like you want to use AJAX to re-render a portion of the page on post back?  Is this correct.

    Are you going to your controller from a view or partial view?  If you go from a view you need to go back to a view, if you go from a partial you need to go back to a partial, thus you need AJAX to rerender that partial.

    I think this is where your problem is.  You are posting back a partial view result using a HTML POST which is re-rendeing the page with just the partial and not the whole page with everything.

    Does that make sense?

    Charlie :)

  • suzyb 474 posts 932 karma points
    Sep 29, 2013 @ 21:19
    suzyb
    0

    It was my intention to have an AJAX post that renders a section on the page.  But as I didn't know how to call the controller from the javascript I thought just making a simple for that posts back to the same page would be easier.  Don't think it was though :p

    What I wanted was the MVC equivelant of an auto postback drop down list where upon selecting an item in the drop down the details for that item are shown on the same page.  I've not done a lot of MVC though and I had a (very) tight deadline so I couldn't take my time and learn how to do it properly.

    If I'm honest I don't really understand what you said.  It makes common sense which is a start I suppose ;)

  • rana 6 posts 75 karma points
    Aug 04, 2015 @ 03:19
    rana
    0

    Dear Suzyb,

    i m struggling to search database data using postback..i m using same technique like you

    TempData["ccgs"] = res;

    but tempdata ccgs is null when i got it in view

    plz help me in this..urgent!

  • suzyb 474 posts 932 karma points
    Aug 04, 2015 @ 08:35
    suzyb
    0

    The only thing I can suggest is to make sure your controller inherits from SurfaceController.

    public class ConstituencySurfaceController : Umbraco.Web.Mvc.SurfaceController
    

    Not sure why TempDate would be null as I don't really understand how my code works :p

  • rana 6 posts 75 karma points
    Aug 04, 2015 @ 08:55
    rana
    0

    hahaha....suzyb...are you not working on this project now...

    you leave umbarco...

    i have completed that using session variable and get session data in view...

    any issue with this?

  • rana 6 posts 75 karma points
    Aug 04, 2015 @ 09:01
    rana
    0

    Thanks for answering ...forget to mention before...

Please Sign in or register to post replies

Write your reply to:

Draft