Copied to clipboard

Flag this post as spam?

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


  • Esmeraldi 9 posts 89 karma points
    Aug 28, 2015 @ 17:20
    Esmeraldi
    0

    Costom controller does not working

    Hi i create a custom controller

            " public class HomeController : RenderMvcController  
       public   ActionResult Index()
                    {
                        var model = new HomeModel();
    
                        return View(model);
                    }
    
    
                }"
    

    and model is

        public class HomeModel : RenderModel
        {
            public HomeModel() : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent) { }
            public string bodytext { get; set; }
            public string headline { get; set; }
    
    }
    

    i create DocumentType at umbraco backoffice with (property ) "bodytext" and another with "headline " and finaly a create content and publish but anything change just whitesite

    home template

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbracos.Models.HomeModel>
    
    @{
        Layout = "Index.cshtml";
    }
    
    <p>@Model.headline</p>
    <p>@Model.bodytext</p>
    

    if i set <p>@Model.Content.GetPropertyValue("Headline")</p> this syill work

    Best Esmeraldi

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Aug 28, 2015 @ 21:51
    Marc Goodson
    1

    try for your controller:

    public class HomeController : RenderMvcController  
       public   ActionResult Index(RenderModel model)
                    {
                        var homeModel = new HomeModel(model.Content, CultureInfo.CurrentCulture);
    
                        return CurrentTemplate(homeModel);
                    }
     }
    

    and for your Model

      public class HomeModel : RenderModel
        {
            public HomeModel(IPublishedContent content, CultureInfo culture) : base(content,culture) { }
            public string bodytext { get; set; }
            public string headline { get; set; }
    
    }
    

    Your custom model contructure can then take in the relevant IPublishedContent and cultureInfo and pass this to the Base RenderModel, so the underlying Umbraco stuff is available via Model.Content but you can then add to your custom properties in the controller

  • Esmeraldi 9 posts 89 karma points
    Aug 29, 2015 @ 12:57
    Esmeraldi
    0

    Thanx marc but still not working....

    best Esmeraldi

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Aug 30, 2015 @ 00:24
    Marc Goodson
    1

    Hi Esmeraldi

    What is the alias of your document type ?

    home ?

    If you set a breakpoint on the controller, does it get hit ? when you make a request to the url for the homepage ?

  • Esmeraldi 9 posts 89 karma points
    Aug 31, 2015 @ 07:50
    Esmeraldi
    0

    Hi Marc

    yes allias is Home if on return actiton is set Return conttnt("test ") it's ok but, i don not understand why model do not working ? Strongly typed view syntax is ok .

  • James Jackson-South 489 posts 1747 karma points c-trib
    Aug 31, 2015 @ 08:01
    James Jackson-South
    0

    Isn't Index a virtual method in RenderMvcController?

    e.g.

    public class HomeController : RenderMvcController  {
    
        public override ActionResult Index(RenderModel model)
        {
           // Marc's code.
        }
    }
    
  • Esmeraldi 9 posts 89 karma points
    Aug 31, 2015 @ 09:50
    Esmeraldi
    0

    James thanx for reply yes i try but nothing.. again i thing the problem is the model can not access property .... :(

  • David Verberckmoes 46 posts 77 karma points
    Aug 31, 2015 @ 12:57
    David Verberckmoes
    0

    Hi,

    add this line to your Controller before returning the View:

            RouteData.DataTokens["umbraco"] = model;
    
  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Sep 01, 2015 @ 00:17
    Marc Goodson
    0

    Also Esmeraldi

    What has the home doctype setup in Umbraco to use as a template ?

    In your code you return View(model) which View is it returning ? and does the home doctype setup for this template ?

    When you set a break point on the controller, has your HomeModel got any properties before you step over and return the view ?

  • Francisco Lino U. Ano 4 posts 74 karma points
    May 05, 2016 @ 12:02
    Francisco Lino U. Ano
    0

    Hi guys, I have the same issue. I have been following the instructions from this docu: https://our.umbraco.org/documentation/reference/routing/custom-controllers as well as from this source: https://gist.github.com/nul800sebastiaan/8641249

    My umbraco version is 7.3.0. I make sure I have created a document type and it has corresponding templates but still I cant get it to work.

    what else could I have missed? any help would be appreciated. thanks!

    Here is my code:

    Controller:

    using prc.web.Models;
    using Umbraco.Web.Mvc;
    using Umbraco.Web.Models;
    
    namespace myproject.Controller
    {
        public class CatalogController: RenderMvcController
        {
            // GET: Books
            public override ActionResult Index(RenderModel model)
            {
                //return View();
                return base.Index(model);
            }
        }
    }
    

    Model :

      using System;
        using System.Collections.Generic;
        using System.ComponentModel.DataAnnotations;
        using System.ComponentModel.DataAnnotations.Schema;
        using System.Linq;
        using System.Runtime.Serialization;
        using Umbraco.Web;
        using Umbraco.Web.Models;
        using Umbraco.Core.Models;
    
        namespace myproject.web.Models
        {
            public class Catalog : RenderModel
            {
                public Catalog(IPublishedContent content)
                    : base(content)
                { }
    
                public int BookId { get; set; }
            }
        }
    

    View:

    @using myproject.web.Models;
    @inherits UmbracoViewPage<Catalog>
    
    @{
        Layout = "Master.cshtml";
    }
    
    <h2>Catalog</h2>
    <p>@Model.BookId</p>
    Custom View Content
    
  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    May 06, 2016 @ 06:51
    Marc Goodson
    0

    Hi Francisco

    It looks like your view is expecting a custom model called Catalog that should inherit from RenderModel.

    Which you have created.

    With the custom BookId Property.

    However when you hijack the route here:

    public override ActionResult Index(RenderModel model)
            {
                //return View();
                return base.Index(model);
            }
    

    You are only passing RenderModel to the view not your custom class.

    Try:

    public override ActionResult Index(RenderModel model)
            {
                //return View();
    var viewModel = new Catalog(model.Content);
    viewModel.BookId = 1234;
    
                return base.Index(viewModel);
            }
    

    to pass your custom model to the view that is expecting it.

    regards

    marc

  • Francisco Lino U. Ano 4 posts 74 karma points
    May 07, 2016 @ 13:29
    Francisco Lino U. Ano
    0

    Hi Marc,

    I tried your code, but my page is still displaying the view coming from the umbraco. :(

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using myproject.web.Models;
    
    @{
        Layout = "Master.cshtml";
    }
    Test - Umbraco View
    
  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    May 07, 2016 @ 21:45
    Marc Goodson
    0

    Hi Francisco

    Your view will need to inherit UmbracoViewPage

    so

    @inherits UmbracoViewPage<Catalog>
    

    regards

    Marc

  • Francisco Lino U. Ano 4 posts 74 karma points
    May 08, 2016 @ 15:16
    Francisco Lino U. Ano
    0

    Did that already. is there any configuration that could allow or disallow custom controller? I did as exactly what the tutorial and your suggestions says but I cant I hijacked any controller. :(

    PS. somebody started this project so I suspect he must have change any config. sorry kinda new to Uumbraco.

    many thanks!

Please Sign in or register to post replies

Write your reply to:

Draft