Full-Stack freelance Umbraco developer in the UK.

George Phillipson - Freelance Umbraco developer

Umbraco 7 Submitting Forms

Umbraco 7 Submitting Forms using post and get
using System.Collections.Generic;

namespace Web.Model.TestData
{
    public class TypeOfPets
    {
        public TypeOfPets(string petTypeId, string petType)
        {
            PetTypeId   = petTypeId;
            PetType     = petType;
        }
        public string PetTypeId { get;}
        public string PetType   { get;}
    }
    public static class TestSearchData
    {
            public static IEnumerable<TypeOfPets> DogList()
            {
                List<TypeOfPets> typesOfPet = new List<TypeOfPets>
                {
                    new TypeOfPets("dog", "German Shepherd"),
                    new TypeOfPets("dog", "Husky"),
                    new TypeOfPets("cat", "Russian Blue"),
                    new TypeOfPets("fish", "Goldfish")
                };
                return typesOfPet;
            }
    }
}
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Web;

namespace Web.Helper.SearchHelper
{
    public static class SearchHelper
    {
        public static string SearchPageUrl()
        {
            var umbContent          = UmbracoAssignedContentHelper.PageContentForIEnumerableIPublishedContent("homePage");
            string searchPageUrl    = umbContent.First().GetPropertyValue<IPublishedContent>("searchPage").Url;

            return searchPageUrl;
        }

        public static string SearchPageSurfaceUrl()
        {
            var umbContent = UmbracoAssignedContentHelper.PageContentForIEnumerableIPublishedContent("homePage");
            string searchPageUrl = umbContent.First().GetPropertyValue<IPublishedContent>("searchSurface").Url;

            return searchPageUrl;
        }
    }
}
using System.Collections.Generic;
using System.Globalization;
using Umbraco.Core.Models;
using Umbraco.Web.Models;
using Web.Model.TestData;

namespace Web.Model.SearchViewModel
{
    public class SearchPageRenderMvcControllerViewModel : RenderModel
    {
        public SearchPageRenderMvcControllerViewModel(IPublishedContent content, CultureInfo culture) : base(content, culture)
        {
        }

        public SearchPageRenderMvcControllerViewModel(IPublishedContent content) : base(content)
        {
        }
        public IEnumerable<TypeOfPets> DisplaySearchResult { get; set; }
    }

    public class SearchPageSurfaceControllerViewModel
    {
        public IEnumerable<TypeOfPets> DisplaySearchResult { get; set; }
    }
}
using System;
using System.Linq;
using System.Web.Mvc;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
using Web.Model.SearchViewModel;
using Web.Model.TestData;

namespace Web.Core.Controllers
{
    public class SearchFormController : RenderMvcController
    {
        [HttpGet]
        public ActionResult SearchForm(RenderModel objModel, string q)
        {
            var queryResult = TestSearchData.DogList().Where(x => string.Equals(x.PetTypeId, q, StringComparison.CurrentCultureIgnoreCase));

            var model = new SearchPageRenderMvcControllerViewModel(objModel.Content)
            {
                DisplaySearchResult = queryResult
            };
            return CurrentTemplate(model);
        }

        [HttpPost]
        public ActionResult SearchForm(RenderModel objModel, string q, string dummyParam)
        {
            var queryResult = TestSearchData.DogList().Where(x => string.Equals(x.PetTypeId, q, StringComparison.CurrentCultureIgnoreCase));

            var model = new SearchPageRenderMvcControllerViewModel(objModel.Content)
            {
                DisplaySearchResult = queryResult
            };
            return CurrentTemplate(model);
        }
    }

    public class SearchFormNotRouteController : SurfaceController
    {
        [HttpGet]
        public ActionResult SearchForm(string q)
        {
            var queryResult = TestSearchData.DogList().Where(x=>string.Equals(x.PetTypeId, q, StringComparison.CurrentCultureIgnoreCase));

            var model = new SearchPageSurfaceControllerViewModel
            {
                DisplaySearchResult = queryResult
            };

            return PartialView("~/Views/Partials/pvSurfaceSearch.cshtml",model);
        }

        [HttpPost]
        public ActionResult SearchForm(string q, string dummyParam)
        {
            var queryResult = TestSearchData.DogList().Where(x => string.Equals(x.PetTypeId, q, StringComparison.CurrentCultureIgnoreCase));

            var model = new SearchPageSurfaceControllerViewModel
            {
                DisplaySearchResult = queryResult
            };
            return PartialView("~/Views/Partials/pvSurfaceSearch.cshtml", model);
        }

        [HttpPost]
        public ActionResult SearchFormUmbraco(string q)
        {
            var queryResult = TestSearchData.DogList().Where(x => string.Equals(x.PetTypeId, q, StringComparison.CurrentCultureIgnoreCase));

            var model = new SearchPageSurfaceControllerViewModel
            {
                DisplaySearchResult = queryResult
            };
            return PartialView("~/Views/Partials/pvSurfaceSearch.cshtml", model);
        }
    }
}
@using Web.Helper.SearchHelper
@inherits UmbracoViewPage
@{
    Layout = "Layout.cshtml";
}
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h1>Search Via RenderMvcController method is GET</h1>
            <form method="get" action="@SearchHelper.SearchPageUrl()" name="RenderMvc" id="RenderMvc">
                <div class="form-group">
                    <input class="form-control" type="text" name="q" id="q" placeholder="Enter keyword 'dog' or 'cat' or 'fish'" />
                </div>
                <br />
                <button class="btn btn-info" type="submit">Search</button>
            </form>
            <h1>Search Via RenderMvcController method is Post</h1>
            <form method="post" action="@SearchHelper.SearchPageUrl()" name="RenderMvcPost" id="RenderMvcPost">
                <div class="form-group">
                    <input class="form-control" type="text" name="q" id="q" placeholder="Enter keyword 'dog' or 'cat' or 'fish'" />
                </div>
                <br />
                <button class="btn btn-success" type="submit">Search</button>
            </form>
            <hr />
            <h1>Search Via SurfaceController method is GET</h1>
            <form method="get" action="@SearchHelper.SearchPageSurfaceUrl()" name="Surface" id="Surface">
                <div class="form-group">
                    <input class="form-control" type="text" name="q" id="q" placeholder="Enter keyword 'dog' or 'cat' or 'fish'" />
                </div>
                <br />
                <button class="btn btn-dark" type="submit">Search</button>
            </form>
            <hr />
            <h1>Search Via SurfaceController method is Post</h1>
            <form method="post" action="@SearchHelper.SearchPageSurfaceUrl()" name="SurfacePost" id="SurfacePost">
                <div class="form-group">
                    <input class="form-control" type="text" name="q" id="q" placeholder="Enter keyword 'dog' or 'cat' or 'fish'" />
                </div>
                <br />
                <button class="btn btn-danger" type="submit">Search</button>
            </form>
            <hr/>
            <h2>Search Via SurfaceController method is Post using Html.BeginUmbracoForm</h2>
            @using (Html.BeginUmbracoForm("SearchFormUmbraco", "SearchFormNotRoute"))
            {
                <div class="form-group">
                    <input class="form-control" type="text" name="q" id="q" placeholder="Enter keyword 'dog' or 'cat' or 'fish'"/>
                </div>
            <br />
            <button class="btn btn-danger" type="submit">Search</button>
            }
 <hr/>
            <h3>Search Via SurfaceController method is Get using Html.BeginUmbracoForm</h3>
            @using (Html.BeginUmbracoForm("SearchFormUmbraco", "SearchFormNotRoute", FormMethod.Get))
            {
                <div class="form-group">
                    <input class="form-control" type="text" name="q" id="q" placeholder="Enter keyword 'dog' or 'cat' or 'fish'"/>
                </div>
                <br />
                <button class="btn btn-danger" type="submit">Search</button>
            }

        </div>
    </div>
</div>

Please enter your comment.