Copied to clipboard

Flag this post as spam?

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


  • Victor 25 posts 146 karma points
    Jan 04, 2017 @ 18:48
    Victor
    0

    Hi, I'm fairly new to umbraco, MVC and C#. I've been struggling to get a form to work with MVC, I'm using ASP.NET and Razor.

    I searched for answers, but couldn't find it. Here is what I want to do:

    I'm using a form to search published content by editors, I managed to make it work a bit by doing what you would do if you use basic PHP, passing the string as a parameter in the URL and working the data on the other page. But I need to do that in MVC.

    I've two pages, Home.cshtml and Search.cshtml, in the Home, I've the form that sends the action to Search.cshtml with GET method, when I tried to do what I did before with MVC, I managed to work out something like this:

    In the home I'm rendering a partial that contains the form.

    Html.RenderPartial("~/Views/Partials/SearchPartial.cshtml", new Search());
    

    In it, I've the following code:

    @model WebSiteBradescoLongevidade.Models.Search
    
            @using (Html.BeginUmbracoForm("Submit", "SearchForm", null, new {@class="search-form"}))
            {
                @Html.LabelFor(m => m.Query, "What do you want to search?", new {@class="focus"})
                @Html.TextBoxFor(m => m.Query)
                <button type="submit">Submit</button>
            }
    

    My model:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace WebSiteBradescoLongevidade.Models
    {
        public class Busca
        {
            public string Query { get; set; }
        }
    }
    

    My controller, I was following this tutorial https://our.umbraco.org/documentation/getting-started/Code/Creating-Forms/ I know I have to work with the data here and make it so it returns only the ones that matches with the search parameter. I once managed to do it using MVC with a database on the project solution, but this time it is Umbraco's database.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Project.Models;
    using Umbraco.Web.Mvc;
    
    
    namespace Project.Controllers
    {
        public class SearchFormController : Umbraco.Web.Mvc.SurfaceController
        {
            // GET: Busca
            public ActionResult Submit(Search model)
            {
                if (!ModelState.IsValid)
                    return CurrentUmbracoPage();
    
    
                return RedirectToCurrentUmbracoPage();
            }
        }
    }
    

    My view that it's going to have the search results, I made it so it would search all the results at first.

    umbraco.NodeFactory.Node node = uQuery.GetNodesByName("Home").FirstOrDefault();
    var publishedFather = node.GetChildNodeByName("Father");
    var publishedChildren = publishedFather.ChildrenAsList;
    
        for (int i = 0; i < publishedFather.ChildrenAsList.Count; i++)
        {
            foreach (var item in publishedChildren[i].ChildrenAsList) 
            {
                @item.GetProperty("title")
                @item.GetProperty("subtitle")
            }
        }
    

    What I did before that it worked, it was the following, from this example:

    http://www.codeshare.co.uk/blog/simple-umbraco-search-example/

    var searchQuery = Request.QueryString["query"];            
    
    foreach (var item in Umbraco.Search(searchQuery))
    {
        @item.GetProperty("title")
        @item.GetProperty("subtitle")
    }
    

    Sorry if the post was long, this is my first post, so if there is any problem with it, I'll fix it on this or the future ones.

    Basically, how do I do that with MVC? Or if you guys could point me out somewhere to learn about this specifically .

    Thank you.

Please Sign in or register to post replies

Write your reply to:

Draft