Copied to clipboard

Flag this post as spam?

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


  • Magnus Söderlund 19 posts 79 karma points
    Jan 14, 2014 @ 10:41
    Magnus Söderlund
    0

    Retaining form values?

    Hello.

    I'm building a search form for my Umbraco-powered site. I've been looking at this documentation: http://our.umbraco.org/documentation/Reference/Mvc/forms/turorial-partial-views but i cant seem to get even that example to work.

    This is how my setup looks (shortened to the relevant code ive added):

    Search.cshtml, my template for the entire search-page:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "Master.cshtml";
    }
    @Html.Partial("SearchForm")

     

    SearchForm.cshtml, my search form partial view:

    @model NameSpace.Models.SearchFormViewModel
    <!-- Searchbox -->
            @using (Html.BeginUmbracoForm<NameSpace.Controllers.SearchFormController>("Search"))
            {
                @Html.TextBoxFor(x => Model.searchText)
    
                <input type="submit" />
            }

     

    SearchFormController.cs, code behind where i want to perform my search later on.

    public class SearchFormController : SurfaceController
        {
            [ChildActionOnly]
            public ActionResult SearchForm()
            {
                return PartialView(new SearchFormViewModel());
            }
    
            [HttpPost]
            public ActionResult Search(SearchFormViewModel model)
            {
                model.searchText = "blurgh";
                return CurrentUmbracoPage();
            }
        }

     

    And finally my modell, SearchFormViewModel.cs: 

    public class SearchFormViewModel
        {
            public string searchText { get; set; }
    
            public IEnumerable<IContent> searchResults;
        }

     

    With this setup i get the following exception:

    An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code

    Additional information: The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type NameSpace.Models.SearchFormViewModel'.

     

    Now, i can get past this by adding the following in my Search.cshtml instead of the above code:

    @Html.Partial("SearchForm", new NameSpace.Models.SearchFormViewModel())

    Which is not how they do it in the documentation, but i can live with that if no one else has a way around this?

     

    Now to the real question:

    I want to submit my search, perform a search in my controller and then return the model with the updated search results so i can redraw the form with all selected values (right now its only text, will be more) and also show the search results. 

     

    One idea i got while writing this question (rubber ducking) was to use TempData, RedirectToCurrentUmbracoPage (should redirect to Search.cshtml right?) and then fill TempData with my model, use that model instead of passing a new one to my Partial View and then also use the SearchResults part for either direct output or by adding another partial?

    Am i on the right track here or is there a completely different approach i should take? Im quite new to Umbraco, MVC, Razor, C# in general so my question might be aimed in the wrong direction. 

  • Magnus Söderlund 19 posts 79 karma points
    Jan 14, 2014 @ 11:56
    Magnus Söderlund
    0

    Also it would be nice with an alternative where i could use GET instead of POST.

    Kind regards

Please Sign in or register to post replies

Write your reply to:

Draft