Copied to clipboard

Flag this post as spam?

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


  • Yarik Goldvarg 35 posts 84 karma points
    Jun 29, 2013 @ 18:48
    Yarik Goldvarg
    0

    MVC - Render Form method=get

    Hello.

    How can i render a form with method="get"

    This code render a form with post method

     @using (Html.BeginUmbracoForm("List", "KnowledgeCenterSurface", null, new { method = "GET" }))

    {

    }

    (I am using umbraco 6.1.2)

    Thanks.

     

     

     

  • Charles Afford 1163 posts 1709 karma points
    Jun 30, 2013 @ 09:15
  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jun 30, 2013 @ 19:35
    Andy Butland
    0

    I believe BeginUmbracoForm is really intended for submitting data - in which case POST is the appropriate method to use.  At least that's what all the documentation and examples refer to.  If you do want a form that submits as GET, perhaps you can just use the normal MVC BeginForm, which has a parameter for setting the form method.

    Andy

  • Charles Afford 1163 posts 1709 karma points
    Jun 30, 2013 @ 19:47
    Charles Afford
    0

    It might be helpfull if you tell us what you are trying to achieve so we can give you an idea on an approite approach :).  Charlie.  I dont know Andy's experice of using the BeginUmbracoForm, but i steer clear and just use AJAX and HTTP. :)

  • Adriano 4 posts 21 karma points
    Jul 10, 2013 @ 13:43
    Adriano
    0

    Hi all,

    I am too facing an identical issue to Yarik. So to explain clearly to you Charlie, here is what I want to achieve and will possibly help Yarik too. You can visit my issue on stackoverflow (http://stackoverflow.com/questions/17560836/http-get-to-return-custom-model-with-data-from-external-database-with-umbraco-mv)

    I am currently working on an Umbraco MVC 4 project version 6.0.5. The project currently uses Vega.USiteBuilder to build the appropriate document types in the backoffice based on strongly typed classes with mapping attributes. Consequently, all my razor files inherit from UmbracoTemplatePageBase<uSiteBuilder.DocumentTypes.ClassName>

     

    I am coming across a road block trying to invoke a HTTP GET from a razor file. For example a search form with multiple fields to submit to a controller action method, using a SurfaceController using Html.BeginUmbracoForm.

     

    My Html.BeginUmbracoForm looks like this

     

        @using (Html.BeginUmbracoForm("FindTyres", "TyreSearch"))

        {

         // Couple of filter fields

        }

     

     I basically have a scenario where I will like to retrieve some records from an external database outside of Umbraco (external to Umbraco Database) and return the results in a custom view model back to my Umbraco front end view. Once my controller and action method is setup to inherit from SurfaceController and thereafter compiling it and submitting the search, I get a 404 resource cannot be found where the requested url specified: /umbraco.RenderMVC.

     

    Here is my code snippet:

     

       

     

        public ActionResult FindTyres(string maker, string years, string models, string vehicles)

                {

                    var tyreBdl = new Wheels.BDL.TyreBDL();

                    List<Tyre> tyres = tyreBdl.GetAllTyres();

        

                    tyres = tyres.Where(t => string.Equals(t.Maker, maker, StringComparison.OrdinalIgnoreCase)

                                             && string.Equals(t.Year, years, StringComparison.OrdinalIgnoreCase)

                                             && string.Equals(t.Model, models, StringComparison.OrdinalIgnoreCase)

                                             && string.Equals(t.Version, vehicles, StringComparison.OrdinalIgnoreCase)).ToList();

        

                    var tyreSearchViewModel = new TyreSearchViewModel

                    {

                        Tyres = tyres

                    };

                    

                    ViewBag.TyreSearchViewModel = tyreSearchViewModel;

                    

                    return CurrentUmbracoPage();

                }

     

    I then resort to using standard MVC, Html.BeginForm (the only difference). Repeating the steps above and submitting the search, I get the following YSOD error.

     

    > Can only use UmbracoPageResult in the context of an Http POST when

    > using a SurfaceController form

     

     

    Below is a snippet of the HTML BeginForm

     

        @using (Html.BeginForm("FindTyres", "TyreSearch"))

        {

         // Couple of filter fields

        }

     

    I feel like I am fighting the Umbraco routes to get my controller to return a custom model back to the razor file. I have googled alot trying to figure out how to do a basic search to return a custom model back to my Umbraco front end view till the extent that I tried to create a custom route but that too did not work for me.

     

    Does my controller need to inherit from a special umbraco controller class to return the custom model back? I will basically like to invoke a HTTP GET request (which is a must) so that my criteria search fields are reflected properly in the query strings of the url. For example upon hitting the search button, I must see the example url in my address browser bar

     

    > http://[domainname]/selecttyres.aspx/TyresSearch/FindTyresMake=ASIA&Years=1994&Models=ROCSTA&Vehicles=261

     

    Therefore, I cannot use Surface Controller as that will operate in the context of a HTTP Post.

     

    Are there good resource materials that I can read up more on umbraco controllers, routes and pipeline.

     

    I hope this scenario makes sense to you. If you have any questions, please let me know. I will need to understand this concept to continue on from here with my project and I do have a deadline.

  • Charles Afford 1163 posts 1709 karma points
    Jul 10, 2013 @ 22:28
    Charles Afford
    0

    Only read this briefly but i have had the same sort of problem i think.  I have always used AJAX and HTML posts rather than using return CurrentPage ect ect.  This has just caused trouble.

    You also need to return a partial view if you make the post from a partial view and view from a post from a view.

    Also a you can do a get using route hijacking.

    What is causing you trouble at the moment?  I know you have posted above, but in a nut shell what are you trying to get over?

    Charlie :)

     

  • Adriano 4 posts 21 karma points
    Jul 11, 2013 @ 02:21
    Adriano
    0

    Hi Charlie,

    My nutshell problem as explained in my post is follows:1

    1. I want to issue a HTTP Get request to obtain some data from an external db outside of umbraco without going through Html.BeginUmbracoForm. Why? Simply because BeginUmbracoForm only works in the context of HTTP POST and I don't get the search fields I will like in query strings in the address url . Note: HTTP GET requests will retain all my search fields in the url but a HTTP Post stores the fields in the request forms collection (which is not what I want to do although this may work). I am also not keen to use AJAX to return my results back. 

    2. Next is that given I am trying to achieve this, I will need to return the result set in a custom model to return back to the umbraco page to render my results by still retaining the UmbracoContext so that my umbraco page renders.

    Hopefully by reading these two points, you know what I want to achieve.

    I am going to experiment again with the route hijacking approach and try to see if that works for me without goung thorugh AJAX or HTML posts.

    Will let you know how I go.

     

  • Adriano 4 posts 21 karma points
    Jul 15, 2013 @ 01:40
    Adriano
    0

    Hi Charlie,

    Route Hijacking worked for me. I did not have to use any form of AJAX and HTML Posts. For my current form on the page, I also did not need use to Child actions. You can have a look at my post here (http://stackoverflow.com/questions/17560836/http-get-to-return-custom-model-with-data-from-external-database-with-umbraco-mv/17644867#17644867)

  • Yarik Goldvarg 35 posts 84 karma points
    Jul 15, 2013 @ 17:31
    Yarik Goldvarg
    0

    So i found another solution.

    Just create a html form with method="get"

     <form method="get">

                <div class="searchInput clearfix">

     

                    @Html.TextBoxFor(model => model.Search, new { placeholder = "Search" })

                    <input type="submit" value="" />

     

                </div>

                <div class="formFields clearfix">

                    <label for="type">@umbraco.library.GetDictionaryItem("TypeTitle")</label>

                    <div class="select197">

                        @Html.DropDownListFor(model => model.Type, Model.Types, new { id = "type" })

                    </div>

                </div>

                <div class="formFields clearfix disabled" id="categoryWrapper">

                    <label for="categoties">@umbraco.library.GetDictionaryItem("CategotiesTitle")</label>

                    <div class="select197 fancy">

                        @if (Model.Categories.Count == 1)

                        {

                            @Html.DropDownListFor(model => model.Category, Model.Categories, new { id = "category", disabled = "disabled" })

                        }

                        else

                        {

                            @Html.DropDownListFor(model => model.Category, Model.Categories, new { id = "category" })

                        }

                    </div>

                </div>

                <a href="javascript:void(0);" title="" class="btn41 R form-submit"><span>GO</span></a>

                <ul class="chceckList clearfix fancy">

                    <li>

                        @Html.CheckBoxFor(model => model.Brochure)

                        @Html.LabelFor(model => model.Brochure, umbraco.library.GetDictionaryItem("BrochureTitle"))

     

                    </li>

     

                    <li>

                        @Html.CheckBoxFor(model => model.Article)

                        @Html.LabelFor(model => model.Article, umbraco.library.GetDictionaryItem("ArticleTitle"))

                    </li>

     

                    <li>

     

     

                        @Html.CheckBoxFor(model => model.CaseStudy)

                        @Html.LabelFor(model => model.CaseStudy, umbraco.library.GetDictionaryItem("CaseStudyTitle"))

     

                    </li>

                    <li>

     

                        @Html.CheckBoxFor(model => model.Movie)

                        @Html.LabelFor(model => model.Movie, umbraco.library.GetDictionaryItem("MovieTitle"))

     

                    </li>

                    <li>

     

                        @Html.CheckBoxFor(model => model.Webinar)

                        @Html.LabelFor(model => model.Webinar, umbraco.library.GetDictionaryItem("WebinarTitle"))

     

     

                    </li>

     

                    <li>

     

                        @Html.CheckBoxFor(model => model.Newsletter)

                        @Html.LabelFor(model => model.Newsletter, umbraco.library.GetDictionaryItem("NewsletterTitle"))

     

                    </li>

                </ul>

                <div class="clear">

                    <!---->

                </div>

     

            </form>

     

     

    and create Surface controller

     

        public class KnowledgeCenterSurfaceController : SurfaceController

        {

            private readonly IKnowledegeCenterService knowledegeCenterService;

            public KnowledgeCenterSurfaceController(IKnowledegeCenterService knowledegeCenterService)

            {

                this.knowledegeCenterService = knowledegeCenterService;

            }

            [HttpGet]

            public ActionResult List(KnowledegeCenterModel model)

            {

                PrepareModel(model);

                return PartialView(model);

            }

    }
Please Sign in or register to post replies

Write your reply to:

Draft