Copied to clipboard

Flag this post as spam?

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


  • Saied 349 posts 674 karma points
    Sep 11, 2015 @ 01:28
    Saied
    0

    RedirectToUmbracoPage with custom model after form post?

    How can I do a POST and RedirectToUmbracoPage() with a custom model. As far as I can tell, RedirectToUmbracoPage takes a nodeId, but I couldn't tell how to pass it my custom model. The model I have does inherit from RenderModel, but I am getting the error that my view expects my custom model, but it was passed a RenderModel.

    Initially, I was just returning View("page"), but this causes another post on refresh, so what I would really like to do is redirect the user? Also, I am not sure if a POST is what I need because I am basically searching a database and returning results, I am actually not saving any data, but I am not sure how to do what I want with a GET?

    In a nutshell, I want to search a database with some Form values, get the data into a model and redirect the user to another page with my model?

    Below works without a custom model, now it is just a matter of getting that in there.

    var nodeId = Convert.ToInt32(CurrentPage.GetProperty("NoProductsFoundUrl").DataValue);
    
    return RedirectToUmbracoPage(nodeId);
    

    Thanks, Saied

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Sep 13, 2015 @ 21:14
    Andy Butland
    0

    It sounds to me that it's probably a GET that you need in this scenario. As you suggest, POSTs are intended (in general web development, not just in Umbraco) for when they trigger a change. For a search, it would sound like a GET would be more appropriate.

    For your scenario I would take a look at route hijacking, and create a controller that responds to the particular doc type/template you are rendering. You can then pass additional parameters to that action method, populate a custom model as you need and return it to your view.

    Hope that helps.

    Andy

  • Saied 349 posts 674 karma points
    Sep 14, 2015 @ 04:16
    Saied
    0

    Hi Andy,

    I accomplished it using a GET with @Html.BeginUmbracoForm, but it appends a upfrt to the url which is really long. I tried doing a normal @Html.BeginForm and form action..., but none of those change the view of the page. The only one that does is @Html.BeginUmbracoForm. Here is the code for the form:

    @model ProductFilterModel
    @using (@Html.BeginUmbracoForm("Search", "ProductSearch", new {@Model }, new { @class = "vehicle-filter group", @id = "productFilterForm" }, FormMethod.Get))
    {
    
    <div class="select">
        <select id="YearDropDownList" name="VehicleYearId" class="year" data-request-url="@Url.Action("GetVehicleYears","ProductFilter")"></select>
        @Html.HiddenFor(x => x.VehicleYear)
    
    </div>
    
    <div class="select">
        <select id="MakeDropDownList" name="VehicleMakeId" class="make" disabled data-request-url="@Url.Action("GetVehicleMakes","ProductFilter")"></select>
        @Html.HiddenFor(x => x.VehicleMake)
    
    
    
    </div>
    
    <div class="select">
        <select id="ModelDropDownList" name="VehicleModelId" class="model" disabled data-request-url="@Url.Action("GetVehicleModels","ProductFilter")"></select>
        @Html.HiddenFor(x => x.VehicleModel)
    
    </div>
    
    
    <div class="select">
        <select id="EngineDropDownList" name="VehicleEngineId" class="engine" disabled data-request-url="@Url.Action("GetVehicleEngines","ProductFilter")"></select>
        @Html.HiddenFor(x => x.VehicleEngine)
        <input type="hidden" id="previousMakeSelected"/>
    </div>
    
    <button id="search" type="submit" class="search-button" disabled>SEARCH</button>
    

    The Search ActionResult method is here:

     public ActionResult Search(ProductFilterModel productFilterModel)
        {
    
            var db = new Database("productsDb");
            var productSearchResultsModel = new ProductSearchResultsModel
            {
                ProductFilterModel = productFilterModel,
                Products = db.Fetch<ProductViewModel>(@"SELECT DISTINCT(p.ProductID) As ProductId, Type, Name, PartNumber, ShortDescription
                                                        FROM Products p
                                                        INNER JOIN productlink pl on pl.ProductID = p.ProductID
                                                        INNER JOIN vehicles v on v.VehicleID = pl.VehicleID
                                                        WHERE Brand = 'SCT'
                                                        AND v.Make = @VehicleMakeId
                                                        AND v.Model = @VehicleModelId
                                                        AND v.Engine = @VehicleEngineId
                                                        AND v.Year = @VehicleYearId", new { productFilterModel.VehicleYearId, productFilterModel.VehicleMakeId, productFilterModel.VehicleModelId, productFilterModel.VehicleEngineId })
            };
    
            if(productSearchResultsModel.Products.Any())
                return View("~/Views/ProductsFound.cshtml",productSearchResultsModel);
    
            return View("~/Views/NoProductsFound.cshtml", productFilterModel);
        }
    

    I noticed if I do a normal form with a GET or do a @Html.BeginForm, it submits the form, but it calls calls javascript and doesn't render the new view with the product results. Any ideas on why this is happening?

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Sep 14, 2015 @ 19:06
    Andy Butland
    0

    Afraid not Saied, sorry. As far as I can see you should just have a form posting a GET request to the product search page. Using Html.BeginForm or just a <form /> element.

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Sep 14, 2015 @ 19:47
    Nik
    1

    Hi Saied,

    Something you could try is putting the model into a TempData field and then on your "no products found" page you check the TempData to see if it contains anything. If it does you use that instead of the model.

  • Saied 349 posts 674 karma points
    Sep 14, 2015 @ 21:41
    Saied
    0

    Nik,

    I thought about doing this, but not wasn't sure if TempData was the right tool for the job?

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Sep 15, 2015 @ 08:06
    Nik
    1

    Temp data is your best bet as is can persist against a change in request. However, you could also try ViewData first.

Please Sign in or register to post replies

Write your reply to:

Draft